Looking to implement better geo-location with Python.
Hostip.info is an open-source project with the goal to build/maintain a database mapping IP addresses to cities. Their about page explains the data sources relied on to populate this database.
Using HostIP, there are two ways to get location data from an IP address:
They also have a well-designed and easy-to-use RESTFUL API: just pass in your ip address after the i***p=*** in the GET request string):
import urllib
response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()
print(response)
Second, the Project Website also makes its complete database available for download.
It is not a Python lib. But http://ipinfodb.com/ provides a webservice that can be easily wrapped by Python code with urllib for example.
http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=74.125.45.100
http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100
You need to request a free API key. See the API doc for details.
You may find these modules useful: MaxMind's GeoIP and its pure version, as well pytz.
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:
- ip
- country_code
- country_name
- region_code
- region_name
- city
- zipcode
- latitude
- longitude
- metro_code
- area_code
Found https://freegeoip.net/; python sample below.
import requests
FREEGEOPIP_URL = 'http://freegeoip.net/json/'
SAMPLE_RESPONSE = """{
"ip":"108.46.131.77",
"country_code":"US",
"country_name":"United States",
"region_code":"NY",
"region_name":"New York",
"city":"Brooklyn",
"zip_code":"11249",
"time_zone":"America/New_York",
"latitude":40.645,
"longitude":-73.945,
"metro_code":501
}"""
def get_geolocation_for_ip(ip):
url = '{}/{}'.format(FREEGEOPIP_URL, ip)
response = requests.get(url)
response.raise_for_status()
return response.json()
IP API is also very nice way to do it.
import urllib
ip= '12.24.36.48'
url = 'http://ip-api.com/json/' + ip
req = urllib.request.Request(url)
out = urllib.request.urlopen(req).read()
I think freegeip is a good option. 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'
I'm using ipinfodb, is free (registration required) and has 2 queries per sec limit and seems to be accurate.
try:
http://api.ipinfodb.com/v3/ip-city/?key={{API_KEY}}&ip=190.188.221.244&timezone=true
returns:
OK;;190.188.221.244;AR;ARGENTINA;BUENOS AIRES;LA PLATA;-;-34.931;-57.949;-03:00
"Geopy makes it easy for developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources, such as wikis.
geopy currently includes support for six geocoders: Google Maps, Yahoo! Maps, Windows Local Live (Virtual Earth), geocoder.us, GeoNames, MediaWiki pages (with the GIS extension), and Semantic MediaWiki pages. "
Install the Python module pygeoip
C:>pip install pygeoip
Download the binary file of GeoLite City from here: https://dev.maxmind.com/geoip/legacy/geolite/
Then:
>>> import pygeoip
>>> g = pygeoip.GeoIP('GeoLiteCity.dat')
>>> ip = '134.222.199.110' #just an example
>>> g.record_by_addr(ip)['latitude']
52.38239999999999
>>> g.record_by_addr(ip)['longitude']
4.899499999999989
>>> g.record_by_addr(ip)['time_zone']
'Europe/Amsterdam'
Contrary to the freegeoip solution I see in the other comments, this option has no limits on the number of IPs per hour, can be used locally without Internet connection, and the geocoordinates are generally more accurate.
You could use requests to make a call to my service https://ipdata.co
import requests
ip = '1.1.1.1'
response = requests.get('https://api.ipdata.co/{}'.format(ip)).json()
response['time_zone']
来源:https://stackoverflow.com/questions/2543018/what-python-libraries-can-tell-me-approximate-location-and-time-zone-given-an-ip