Get physical position of device with Python?

前端 未结 5 2144
深忆病人
深忆病人 2021-01-20 11:51

Is there a way to get the computer\'s physical position using Python, preferably without an API, or with a free API? I\'ve searched around a bit, and the only free API I\'ve

5条回答
  •  情书的邮戳
    2021-01-20 12:04

    import urllib2
    import json
    
    # Automatically geolocate the connecting IP
    f = urllib2.urlopen('http://freegeoip.net/json/')
    json_string = f.read()
    f.close()
    location = json.loads(json_string)
    print(location)
    location_city = location['city']
    location_state = location['region_name']
    location_country = location['country_name']
    location_zip = location['zipcode']
    

    Send HTTP GET requests to: freegeoip.net/{format}/{ip_or_hostname} to receive a JSON output that Python can parse.

    I get the following JSON keys, which should be sufficient for what you are needing:

    • ip
    • country_code
    • country_name
    • region_code
    • region_name
    • city
    • zipcode
    • latitude
    • longitude
    • metro_code
    • area_code

提交回复
热议问题