What python libraries can tell me approximate location and time zone given an IP address?


Looking to implement better geo-location with Python.

Asked By: ensnare



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.


You need to request a free API key. See the API doc for details.

Answered By: luc

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=').read()


Second, the Project Website also makes its complete database available for download.

Answered By: doug

You may find these modules useful: MaxMind’s GeoIP and its pure version, as well pytz.

Answered By: ony

I’m using ipinfodb, is free (registration required) and has 2 queries per sec limit and seems to be accurate.




OK;;;AR;ARGENTINA;BUENOS AIRES;LA PLATA;-;-34.931;-57.949;-03:00

Answered By: sebastian serrano

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.

Answered By: Kylotan

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/'
    with closing(urlopen(url)) as response:
        location = json.loads(response.read())
        location_city = location['city']
        location_state = location['region_name']
        location_country = location['country_name']
        location_zip = location['zipcode']
    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
Answered By: Jason Parham

Found https://freegeoip.net/; python sample below.

import requests

FREEGEOPIP_URL = 'http://freegeoip.net/json/'

    "country_name":"United States",
    "region_name":"New York",

def get_geolocation_for_ip(ip):
    url = '{}/{}'.format(FREEGEOPIP_URL, ip)

    response = requests.get(url)

    return response.json()
Answered By: berto

IP API is also very nice way to do it.

import urllib

ip= ''

url = 'http://ip-api.com/json/' + ip

req = urllib.request.Request(url)

out = urllib.request.urlopen(req).read()

enter image description here

Answered By: Shubham Chaudhary

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

>>> import requests
>>> ip = ''   
>>> url = 'http://freegeoip.net/json/'+ip
>>> r = requests.get(url)
>>> js = r.json()
>>> js['country_code']
>>> js['country_name']
>>> js['time_zone']
Answered By: Waqar Detho

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/


>>> import pygeoip
>>> g = pygeoip.GeoIP('GeoLiteCity.dat')
>>> ip = '' #just an example
>>> g.record_by_addr(ip)['latitude']
>>> g.record_by_addr(ip)['longitude']
>>> g.record_by_addr(ip)['time_zone']

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.

Answered By: Ideophagous

You could use requests to make a call to my service https://ipdata.co

import requests

ip = ''
response = requests.get('https://api.ipdata.co/{}'.format(ip)).json()
Answered By: Jonathan

Maxmind has an official api package that integrates with their geoip web service or geolite databases.
You'll have to either download their free geolite database or create a free trial account.

If you register for an account you'll get $5 in promotional credit (paid queries cost about $0.0001 each). Login to your account, click on 'My License Key' and create a license key.

Then install the package:

$ pip install geoip2

And test the api:

Test function adapted from the docs

import geoip2.webservice

account_id = 1 # enter your real account id here
api_key = "enter your real api/license key here"

TEST_IP = ''

def lookup_ip(ip=TEST_IP):
    # This creates a Client object that can be reused across requests.
    client = geoip2.webservice.Client(account_id, api_key)

    # Replace "insights" with the method corresponding to the web service
    # that you are using, e.g., "country", "city".
    response = client.insights(ip)

    print("country iso code: {0}".format(response.country.iso_code))

    print("country name: {0}".format(response.country.name))

    print("subdivisions most specific name: {0}".format(response.subdivisions.most_specific.name))
    print("subdivisions most specific iso code: {0}".format(response.subdivisions.most_specific.iso_code))

    print("city name: {0}".format(response.city.name))

    print("postal code:{0}".format(response.postal.code))

    print("coordinates: ({0}, {1})".format(response.location.latitude, response.location.longitude))

    return response

if __name__ == "__main__":
    resp = lookup_ip()
Answered By: user2682863

I had replied to similar question at here: How to find location with IP address in Python?, so let me put the answer on here also with the installation step.

Assuming that you got the ip address already, you can try to use the IP2Location Python Library to get the user location. First of all you would have to install the IP2Location Python Library. You can install it by using PyPi: pip install IP2Location, or download the release from here: https://github.com/chrislim2888/IP2Location-Python/releases. After that, you can use the library along with the database downloaded from IP2Location or IP2Location LITE like this:

import os
import IP2Location

database = IP2Location.IP2Location(os.path.join("data", "IP2LOCATION-LITE-DB11.BIN"))

rec = database.get_all(ip)


Depends on your requirement, for example if you want to get the user's country name and region name, you can do this:

import os
import IP2Location

database = IP2Location.IP2Location(os.path.join("data", "IP2LOCATION-LITE-DB11.BIN"))

rec = database.get_all(ip)

user_country = rec.country_long
user_region = rec.region

For more details, you can visit here: https://www.ip2location.com/developers/python

Github link: https://github.com/chrislim2888/IP2Location-Python

Answered By: ooi18

IPinfo provides you with both the location information and timezone on one single API call. They also have a Python Library.

Method 1: Using the API with requests

import requests

# here I have genearated this IP address randomly
ip_address = ""

# get your token from your IPinfo account dashboard
token = ""

# using f-string here. Use any method to create the URL
url = f"https://ipinfo.io/{ip_address}?token={token}"

# json response after the IP lookup
response = requests.get(url).json()

The response will look like this:

  "ip": "",
  "city": "Tianjin",
  "region": "Tianjin",
  "country": "CN",
  "loc": "39.1422,117.1767",
  "org": "AS4837 CHINA UNICOM China169 Backbone",
  "timezone": "Asia/Shanghai"

You can get your location and timezone from the response.

>>> response['city']

>>> response['country']

>>> response['timezone']

Method 2: Using the Python library

The IPinfo Python library makes this process much easier. Install it with pip. IPinfo Python Documentation

# install with `pip install ipinfo`
import ipinfo

# initialize handler with access token
access_token = "insert_your_token_here"
handler = ipinfo.getHandler(access_token)

ip_address = ''
details = handler.getDetails(ip_address)

From the details you can now find individual information

>>> details.city
'Mountain View'

>>> details.timezone
Answered By: anyfactor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.