From 1a1b5d506a57202aa3140cf178ecd4d55a5b85f8 Mon Sep 17 00:00:00 2001 From: Beh185 Date: Mon, 4 Sep 2023 11:40:48 +0330 Subject: [PATCH] Updated --- ipaddress/README.md | 18 ++++- ipaddress/main.py | 65 +++++++++++++------ .../{requirments.txt => requirements.txt} | 0 3 files changed, 62 insertions(+), 21 deletions(-) rename ipaddress/{requirments.txt => requirements.txt} (100%) diff --git a/ipaddress/README.md b/ipaddress/README.md index ef73531..99fed14 100644 --- a/ipaddress/README.md +++ b/ipaddress/README.md @@ -1,3 +1,19 @@ # IP Address -This scripts will print IP address of machine and info realted to the IP address . +This scripts will give you some useful information about your ip address. For instance: +- IP address 🧭 +- Continent 🌍 +- Country 🗾 +- City 🏘️ +- Area 🏠 +- Postal code 📮 +- Isp 🏢 +- Using Mobile 📱 +- Using Proxy 🌐 + +## Installation +After downloading the script you can simply install requirement by:\ +> `pip install -r requirements.txt` + +and then +> `python main.py` \ No newline at end of file diff --git a/ipaddress/main.py b/ipaddress/main.py index 7d2627c..6e6a5d5 100644 --- a/ipaddress/main.py +++ b/ipaddress/main.py @@ -1,27 +1,52 @@ -import requests +# Import Part +from os import system +try: + from requests import get, Response, ReadTimeout +except ImportError: + system('pip install -r requirements.txt') + exit('Please run the program again') + +# Api +ip_provider = r"https://api.techniknews.net/ipgeo/" + +print('Getting ip info. It\'s may take some time ...') -ip_provider = "http://ipaddr.in/json" -user_agent = { - 'User-agent': 'curl/7.82.0' - } try : - web_request = requests.get(ip_provider, headers=user_agent , timeout=10) - response = web_request.json() - - ip = response["ip"] - city = response["city"] - area = response["region_name"] - zip_code = response["zip_code"] - country = response["country"] + response: Response = get(ip_provider, timeout=20) + if response.status_code != 200: + exit(f"[{response.status_code} Error]") + + # Getting info from json file + JsonResponse: dict = response.json() + if JsonResponse['status'] == 'success': + ip: str = JsonResponse["ip"] + city: str = JsonResponse["city"] + area: str = JsonResponse["regionName"] + zip_code: str = JsonResponse["zip"] + country: str = JsonResponse["country"] + continent: str = JsonResponse['continent'] + isp: str = JsonResponse['isp'] + IsMobile: bool = JsonResponse['mobile'] + UseProxy: bool = JsonResponse['proxy'] + else: + exit('The operation was unsuccessful. Try again') print( f""" - IP address : {ip} - City : {city} - Area : {area} - Postal code : {zip_code} - Country : {country} + IP address : {ip} + Continent : {continent} + Country : {country} + City : {city} + Area : {area} + Postal code : {zip_code} + Isp : {isp} + Using Mobile : {IsMobile} + Using Proxy : {UseProxy} """ ) -except : - print("Error !! Check your internt connection") \ No newline at end of file +except ConnectionError: + exit("Error !! Check your internet connection") +except KeyboardInterrupt: + exit('The operation canceled by user') +except ReadTimeout: + exit('Timeout has reached') \ No newline at end of file diff --git a/ipaddress/requirments.txt b/ipaddress/requirements.txt similarity index 100% rename from ipaddress/requirments.txt rename to ipaddress/requirements.txt