Which python libraries can tell me the approximate location and timezone given the IP address?

Looking for an implementation of better geolocation with Python.

+44
python timezone geolocation
Mar 30 '10 at 5:22
source share
11 answers

Hostip.info is an open source project with the goal of creating / maintaining a database mapping IP addresses in cities . Their page talks about the data sources that were used 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 your IP address after I *** p = *** in the GET request line):

import urllib response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read() print(response) 

Secondly, the project website also provides a complete database for download.

+35
Mar 30 '10 at 5:59
source share
β€” -

This is not a Python lib. But http://ipinfodb.com/ provides a web service that can be easily wrapped with Python code using 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.

+13
Mar 30 '10 at 5:55
source share

You may find these modules useful: MaxMind GeoIP and the clean version , as well as pytz .

+8
Mar 30 '10 at 8:12
source share

I posted this on another question that was 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/{formatasket/{ip_or_hostname} to get JSON output that Python can handle.

I get the following JSON keys, which should be enough for what you need:

  • f
  • country_code
  • COUNTRY_NAME
  • region_code
  • REGION_NAME
  • city
  • postcode
  • latitude
  • longitude
  • metro_code
  • city ​​code
+7
Oct 02 '14 at 16:42
source share

Found https://freegeoip.net/ ; python 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() 
+5
Dec 08 '14 at 21:35
source share

IP APIs are also a very good way to do this.

enter image description here

+4
Jan 27 '15 at 8:20
source share

I think freegeip is a good option. Below is the Python 3.4.2 code for getting location and timezone.

 >>> 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' 
+2
Jun 15 '16 at 8:22
source share

I use ipinfodb, it is free (registration is required) and has 2 requests for 1 second and seems accurate.

to 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

+1
Apr 27 2018-11-11T00:
source share

" Geopy allows developers to find coordinates for addresses, cities, countries, and landmarks around the world, third-party geocoders, and other data sources such as wikis.

Currently, geography includes support for six geocoders: Google Maps, Yahoo! Maps, Windows Local Live (Virtual Earth), geocoder.us, GeoNames, MediaWiki (with GIS extension) and Semantic MediaWiki pages. "

+1
Mar 01 '12 at 10:13
source share

Install python pygeoip module

 C:>pip install pygeoip 

Download the GeoLite City binary 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'

Unlike the freegeoip solution, which I see in other comments, this parameter has no restrictions on the number of IP addresses per hour, can be used locally without an Internet connection, and geo-coordinates are usually more accurate.

0
Jun 04 '17 at 18:46 on
source share

You can use the 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'] 
0
Sep 04 '17 at 22:06 on
source share



All Articles