Get physical position of device with Python?

前端 未结 5 2080
深忆病人
深忆病人 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:09

    If you know the public IP of the device then you can do so by using freegeip. Below is the Python 3.4.2 code for getting location and time zone.

    >>> import requests
    >>> ip = '141.70.111.66'
    >>> url = 'http://freegeoip.net/json/'+ip
    >>> r = requests.get(url)
    >>> js = r.json()
    >>> js['country_code']
    'DE'
    >>> js['country_name']
    'Germany'
    >>> js['time_zone']
    'Europe/Berlin'
    >>> js['city']
    'Stuttgart'
    >>> js.items()
    dict_items([('latitude', 48.7667), ('ip', '141.70.111.66'), ('region_code', 'BW'), ('country_code', 'DE'), ('city', 'Stuttgart'), ('zip_code', '70173'), ('country_name', 'Germany'), ('longitude', 9.1833), ('region_name', 'Baden-Württemberg Region'), ('time_zone', 'Europe/Berlin'), ('metro_code', 0)])
    

提交回复
热议问题