Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Pure Python API for Maxmind's binary GeoIP databases
branch: master

This branch is 175 commits behind appliedsec:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

Pure Python GeoIP API

The API is based on MaxMind's C-based Python API, but the code itself is ported from the Pure PHP GeoIP API by Jim Winstead and Hans Lellelid.

It is mostly a drop-in replacement, except the new and open methods are gone.

Tested using tox with Python version 2.5, 2.6, 2.7, 3.1, 3.2 and 3.3.

Issues and contribution

Bug reports are done by creating an issue on Github. If you want to contribute you can always create a pull request for discussion and code submission.


You can easily install pygeoip with setuptools:

easy_install pygeoip

Supported Databases

  • Country
  • Region
  • City
  • Organization
  • ISP
  • ASN

Quick Documentation

Create your GeoIP instance with appropriate access flag. STANDARD reads data from disk when needed, MEMORY_CACHE loads database into memory on instantiation and MMAP_CACHE loads database into memory using mmap.

import pygeoip
gi = pygeoip.GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE)

Country lookup

>>> gi.country_code_by_name('')
>>> gi.country_code_by_addr('')
>>> gi.country_name_by_addr('')
'United States'

Region lookup

>>> gi = pygeoip.GeoIP('/path/to/GeoIPRegion.dat')
>>> gi.region_by_name('')
{'region_name': 'CA', 'country_code': 'US'}

City lookup

>>> gi = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gi.record_by_addr('')
    'city': 'Mountain View',
    'region_name': 'CA',
    'area_code': 650,
    'longitude': -122.0574,
    'country_code3': 'USA',
    'latitude': 37.419199999999989,
    'postal_code': '94043',
    'dma_code': 807,
    'country_code': 'US',
    'country_name': 'United States'
>>> gi.time_zone_by_addr('')

Organization lookup

>>> gi = pygeoip.GeoIP('/path/to/GeoIPOrg.dat')
>>> gi.org_by_name('')
'Dell Computer Corporation'

ISP lookup

>>> gi = pygeoip.GeoIP('/path/to/GeoIPISP.dat')
>>> gi.org_by_name('')
'Turner Broadcasting System'

ASN lookup

>>> gi = pygeoip.GeoIP('/path/to/GeoIPASNum.dat')
>>> gi.org_by_name('')
'AS5662 Turner Broadcasting'

For more information, check out the full API documentation.

Something went wrong with that request. Please try again.