Looking to implement better geo-location with Python.
I posted this in another question that had been buried, but linked here:
#!/usr/bin/env python
from urllib2 import urlopen
from contextlib import closing
import json
# Automatically geolocate the connecting IP
url = 'http://freegeoip.net/json/'
try:
with closing(urlopen(url)) as response:
location = json.loads(response.read())
print(location)
location_city = location['city']
location_state = location['region_name']
location_country = location['country_name']
location_zip = location['zipcode']
except:
print("Location could not be determined automatically")
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: