How To Know Anyone’s Location Using IP Address With Python

How To Know Anyone’s Location Using IP Address With Python

CODE

import requests

def get_location_by_ip(ip_address):
    url = f"http://ipinfo.io/{ip_address}/json"
    response = requests.get(url)

    if response.status_code == 200:
        data = response.json()
        city = data.get('city', 'N/A')
        region = data.get('region', 'N/A')
        country = data.get('country', 'N/A')
        loc = data.get('loc', 'N/A').split(',')
        latitude, longitude = loc[0], loc[1]

        print(f"IP Address: {ip_address}")
        print(f"City: {city}")
        print(f"Region: {region}")
        print(f"Country: {country}")
        print(f"Latitude: {latitude}")
        print(f"Longitude: {longitude}")
    else:
        print("Failed to retrieve the location data.")

if __name__ == "__main__":
    ip_address = input("Enter the IP address to get the location: ")
    get_location_by_ip(ip_address)

Results

Enter the IP address to get the location: xxxxxxxxx
IP Address: xxxxxxx
City: xx
Region: xx
Country: xx
Latitude: xx.xxxx
Longitude: xx.xxxx

How To Know Anyone’s Location Using IP Address With Python

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top