From a0c023209022de991738ec6ced71e4d0c8f5ac05 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:30:48 +0530 Subject: [PATCH 1/6] Create get_ip_geolocation.py --- web_programming/get_ip_geolocation.py | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 web_programming/get_ip_geolocation.py diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py new file mode 100644 index 000000000000..a9ca133804ea --- /dev/null +++ b/web_programming/get_ip_geolocation.py @@ -0,0 +1,32 @@ +import requests + +# Function to get geolocation data for an IP address +def get_ip_geolocation(ip_address): + try: + # Construct the URL for the IP geolocation API + url = f"https://ipinfo.io/{ip_address}/json" + + # Send a GET request to the API + response = requests.get(url) + + # Parse the response as JSON + data = response.json() + + # Check if city, region, and country information is available + if 'city' in data and 'region' in data and 'country' in data: + location = f"Location: {data['city']}, {data['region']}, {data['country']}" + else: + location = "Location data not found." + + return location + except Exception as e: + # Handle exceptions, such as network errors or invalid IP addresses + return str(e) + +if __name__ == "__main__": + # Prompt the user to enter an IP address + ip_address = input("Enter an IP address: ") + + # Get the geolocation data and print it + location = get_ip_geolocation(ip_address) + print(location) From 45a1d57200b3936c6db45deb47576a13fdd82c4c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:02:23 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_ip_geolocation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py index a9ca133804ea..ae08c0b4455d 100644 --- a/web_programming/get_ip_geolocation.py +++ b/web_programming/get_ip_geolocation.py @@ -1,19 +1,20 @@ import requests + # Function to get geolocation data for an IP address def get_ip_geolocation(ip_address): try: # Construct the URL for the IP geolocation API url = f"https://ipinfo.io/{ip_address}/json" - + # Send a GET request to the API response = requests.get(url) - + # Parse the response as JSON data = response.json() # Check if city, region, and country information is available - if 'city' in data and 'region' in data and 'country' in data: + if "city" in data and "region" in data and "country" in data: location = f"Location: {data['city']}, {data['region']}, {data['country']}" else: location = "Location data not found." @@ -23,10 +24,11 @@ def get_ip_geolocation(ip_address): # Handle exceptions, such as network errors or invalid IP addresses return str(e) + if __name__ == "__main__": # Prompt the user to enter an IP address ip_address = input("Enter an IP address: ") - + # Get the geolocation data and print it location = get_ip_geolocation(ip_address) print(location) From 53c10c8dd4f14441d8ead6e600df108bad540258 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:33:48 +0530 Subject: [PATCH 3/6] Update get_ip_geolocation.py --- web_programming/get_ip_geolocation.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py index ae08c0b4455d..d5c2804dbfb5 100644 --- a/web_programming/get_ip_geolocation.py +++ b/web_programming/get_ip_geolocation.py @@ -1,20 +1,19 @@ import requests - # Function to get geolocation data for an IP address -def get_ip_geolocation(ip_address): +def get_ip_geolocation(ip_address: str) -> str: try: # Construct the URL for the IP geolocation API url = f"https://ipinfo.io/{ip_address}/json" - + # Send a GET request to the API response = requests.get(url) - + # Parse the response as JSON data = response.json() # Check if city, region, and country information is available - if "city" in data and "region" in data and "country" in data: + if 'city' in data and 'region' in data and 'country' in data: location = f"Location: {data['city']}, {data['region']}, {data['country']}" else: location = "Location data not found." @@ -24,11 +23,10 @@ def get_ip_geolocation(ip_address): # Handle exceptions, such as network errors or invalid IP addresses return str(e) - if __name__ == "__main__": # Prompt the user to enter an IP address ip_address = input("Enter an IP address: ") - + # Get the geolocation data and print it location = get_ip_geolocation(ip_address) print(location) From bbba8ce39bad9facc086a2ca974f7ef714b211bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:04:31 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_ip_geolocation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py index d5c2804dbfb5..25ea09379e62 100644 --- a/web_programming/get_ip_geolocation.py +++ b/web_programming/get_ip_geolocation.py @@ -1,19 +1,20 @@ import requests + # Function to get geolocation data for an IP address def get_ip_geolocation(ip_address: str) -> str: try: # Construct the URL for the IP geolocation API url = f"https://ipinfo.io/{ip_address}/json" - + # Send a GET request to the API response = requests.get(url) - + # Parse the response as JSON data = response.json() # Check if city, region, and country information is available - if 'city' in data and 'region' in data and 'country' in data: + if "city" in data and "region" in data and "country" in data: location = f"Location: {data['city']}, {data['region']}, {data['country']}" else: location = "Location data not found." @@ -23,10 +24,11 @@ def get_ip_geolocation(ip_address: str) -> str: # Handle exceptions, such as network errors or invalid IP addresses return str(e) + if __name__ == "__main__": # Prompt the user to enter an IP address ip_address = input("Enter an IP address: ") - + # Get the geolocation data and print it location = get_ip_geolocation(ip_address) print(location) From 29f9b05cdfea91a843a7b01f3c001408f8d621a3 Mon Sep 17 00:00:00 2001 From: SEIKH NABAB UDDIN <93948993+nababuddin@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:40:33 +0530 Subject: [PATCH 5/6] Update get_ip_geolocation.py --- web_programming/get_ip_geolocation.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py index 25ea09379e62..9db21594337f 100644 --- a/web_programming/get_ip_geolocation.py +++ b/web_programming/get_ip_geolocation.py @@ -1,34 +1,38 @@ import requests - # Function to get geolocation data for an IP address def get_ip_geolocation(ip_address: str) -> str: try: # Construct the URL for the IP geolocation API url = f"https://ipinfo.io/{ip_address}/json" - + # Send a GET request to the API response = requests.get(url) - + + # Check if the HTTP request was successful + response.raise_for_status() + # Parse the response as JSON data = response.json() # Check if city, region, and country information is available - if "city" in data and "region" in data and "country" in data: + if 'city' in data and 'region' in data and 'country' in data: location = f"Location: {data['city']}, {data['region']}, {data['country']}" else: location = "Location data not found." return location - except Exception as e: - # Handle exceptions, such as network errors or invalid IP addresses - return str(e) - + except requests.exceptions.RequestException as e: + # Handle network-related exceptions + return f"Request error: {e}" + except ValueError as e: + # Handle JSON parsing errors + return f"JSON parsing error: {e}" if __name__ == "__main__": # Prompt the user to enter an IP address ip_address = input("Enter an IP address: ") - + # Get the geolocation data and print it location = get_ip_geolocation(ip_address) print(location) From 55ca6cd44d67f3f45a8249e0f929ddf214c294a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:11:12 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/get_ip_geolocation.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/web_programming/get_ip_geolocation.py b/web_programming/get_ip_geolocation.py index 9db21594337f..62eaeafceb7e 100644 --- a/web_programming/get_ip_geolocation.py +++ b/web_programming/get_ip_geolocation.py @@ -1,22 +1,23 @@ import requests + # Function to get geolocation data for an IP address def get_ip_geolocation(ip_address: str) -> str: try: # Construct the URL for the IP geolocation API url = f"https://ipinfo.io/{ip_address}/json" - + # Send a GET request to the API response = requests.get(url) - + # Check if the HTTP request was successful response.raise_for_status() - + # Parse the response as JSON data = response.json() # Check if city, region, and country information is available - if 'city' in data and 'region' in data and 'country' in data: + if "city" in data and "region" in data and "country" in data: location = f"Location: {data['city']}, {data['region']}, {data['country']}" else: location = "Location data not found." @@ -29,10 +30,11 @@ def get_ip_geolocation(ip_address: str) -> str: # Handle JSON parsing errors return f"JSON parsing error: {e}" + if __name__ == "__main__": # Prompt the user to enter an IP address ip_address = input("Enter an IP address: ") - + # Get the geolocation data and print it location = get_ip_geolocation(ip_address) print(location)