Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
jennifere edited this page Jan 9, 2012 · 5 revisions

This is a pure Python alternative to MaxMind's C-based Python API for binary GeoIP databases. It is intended to make it easier to develop on Windows, or to simplify dependencies for things that don't require the speed/features of the C API. It is not entirely a drop-in replacement, but the most notable difference is the new and open methods are gone, in favor of instantiating the GeoIP class directly. The databases aren't distributed with the module, so you'll need to either buy them or download a lite edition here. Requires Python 2.5 or higher (including Python 3).

Supported Flags

  • STANDARD (do all reads from disk, slowest)
  • MEMORY_CACHE
  • MMAP_CACHE (map the whole file to memory, fastest)

Supported Databases

  • Country (Methods: country_code_by_name, country_code_by_addr, country_name_by_name, country_name_by_addr)
  • City (Methods: record_by_name, record_by_addr, country_code_by_name, country_code_by_addr, country_name_by_name, country_name_by addr, region_by_name, region_by_addr, time_zone_by_addr, time_zone_by_name)
  • ISP (Methods: org_by_name, org_by_addr)
  • Organization (Methods: org_by_name, org_by_addr)
  • Region (Methods: region_by_name, region_by_addr, country_code_by_name, country_code_by_addr, time_zone_by_addr, time_zone_by_name)

Installation

If you have setuptools installed, you can just run:

# easy_install pygeoip

If not, you can just download the source and run:

# python setup.py install

Usage

Below are some examples of usage. You can view the latest API docs here.

Country Database (GeoIP.dat)

>>> import pygeoip
>>> gi = pygeoip.GeoIP('/path/to/GeoIP.dat')
>>> gi.country_code_by_name('google.com')
'US'
>>> gi.country_code_by_addr('64.233.161.99')
'US'
>>> gi.country_name_by_name('google.com')
'United States'
>>> gi.country_name_by_addr('64.233.161.99')
'United States'

City Database (GeoIPCity.dat)

>>> import pygeoip
>>> gic = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gic.record_by_addr('64.233.161.99')
{'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'}
>>> gic.record_by_name('google.com')
{'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'}
>>> gic.region_by_name('google.com')
{'region_name': 'CA', 'country_code': 'US'}
>>> gic.region_by_addr('64.233.161.99')
{'region_name': 'CA', 'country_code': 'US'}
>>> gic.time_zone_by_name('google.com')
'America/Los_Angeles'
>>> gic.time_zone_by_addr('64.233.161.99')
'America/Los_Angeles'

Org/ISP Database (GeoIPOrg.dat or GeoIPISP.dat)

>>> import pygeoip
>>> gio = pygeoip.GeoIP('/path/to/GeoIPOrg.dat')
>>> gio.org_by_addr('64.233.161.99')
'Google'
>>> gio.org_by_name('google.com')
'Google'

Region Database (GeoIPRegion.dat)

>>> import pygeoip
>>> gir = pygeoip.GeoIP('/path/to/GeoIPRegion.dat')
>>> gir.region_by_name('yahoo.com')
{'region_name': 'CA', 'country_code': 'US'}
>>> gir.region_by_addr('209.131.36.159')
{'region_name': 'CA', 'country_code': 'US'}