skip to Main Content

I want to get the location from windows

I tried to get the loc from IP using ipinfo.io but the info is vaild so i want to take the loc from windows

I just want how to get tje location
I didn’t found any thing on the internet

2

Answers


  1. This works for me

    import requests
    
    def get_global_ip():
        try:
            response = requests.get('https://ipinfo.io/json')
            data = response.json()
            return data['ip'], data['country'], data['city']
        except Exception as e:
            print("Error while retrieving location:", e)
            return None
    
    global_ip = get_global_ip()
    if global_ip:
        print("Your global IP address and location:", global_ip)
    else:
        print("Failed to retrieve the global IP address and location")
    

    or if you know your ip you can try this:

    import requests
    
    def get_location(ip):
        url = f"http://ip-api.com/json/{ip}"
        response = requests.get(url)
        data = response.json()
        if data['status'] == 'success':
            return data['city'], data['country']
        else:
            return None
    
    my_ip = 'xxx.xxx.xxx.xxx' # global/white ip-address
    
    location = get_location(my_ip)
    if location:
        city, country = location
        print(f"Your city: {city}, country: {country}")
    else:
        print("It is not possible to get info about your location :(")
    
    
    Login or Signup to reply.
  2. If you want to use the IP2Location.io free API, the below code will work for your public IP address.

    import requests
    
    def get_my_public_ip():
        try:
            response = requests.get('https://api.ip2location.io/')
            data = response.json()
            return data['ip'], data['country_code'], data['country_name'], data['region_name'], data['city_name'], data['latitude'], data['longitude'], data['zip_code'], data['time_zone'], data['asn'], data['as'], data['is_proxy']
        except Exception as e:
            print("Error while retrieving location:", e)
            return None
    
    my_public_ip = get_my_public_ip()
    if my_public_ip:
        print("Your public IP address and location:", my_public_ip)
    else:
        print("Failed to retrieve the public IP address and location")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search