function to get geolocation data for an ip address construct the url for the ip geolocation api send a get request to the api check if the http request was successful parse the response as json check if city region and country information is available handle networkrelated exceptions handle json parsing errors prompt the user to enter an ip address get the geolocation data and print it function to get geolocation data for an ip address construct the url for the ip geolocation api send a get request to the api check if the http request was successful parse the response as json check if city region and country information is available handle network related exceptions handle json parsing errors prompt the user to enter an ip address get the geolocation data and print it
import requests



def get_ip_geolocation(ip_address: str) -> str:
    try:
        
        url = f"https://ipinfo.io/{ip_address}/json"

        
        response = requests.get(url)

        
        response.raise_for_status()

        
        data = response.json()

        
        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 requests.exceptions.RequestException as e:
        
        return f"Request error: {e}"
    except ValueError as e:
        
        return f"JSON parsing error: {e}"


if __name__ == "__main__":
    
    ip_address = input("Enter an IP address: ")

    
    location = get_ip_geolocation(ip_address)
    print(location)