My favorites | Sign in
Project Home Downloads Wiki
Search
for
Usage  
Usage and examples
Featured, Phase-Implementation
Updated Sep 22, 2011 by zay...@gmail.com

Below are some examples of usage. You can download 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'}
Comment by Auston.B...@gmail.com, Sep 28, 2010

Thank you for this! Just in case no one else has.

Comment by run...@gmail.com, Dec 7, 2010

HI, it does not seem to match the geoip install on OSX via macports. I get a module named GeoIP Which has functions new and open. Under py26

Comment by run...@gmail.com, Dec 7, 2010

Example in python2.6 shows:

#!/usr/bin/python

import GeoIP

gi = GeoIP.open("/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD) gir = gi.record_by_name("www.google.com")

Comment by project member zay...@gmail.com, Jun 5, 2011

There is a geoip module by maxmind that's written in C, which has new and open functions. It's not the same thing.

Comment by christia...@gmail.com, Jun 11, 2011

Thanks! This is awesome!

Comment by datacor...@gmail.com, Jul 27, 2011

Nice work. Now... what if you get_country_by_name and the hostname resolves to more than one IP. Does it returns the country of each IP?

Comment by margus.h...@gmail.com, Nov 16, 2011

Sweet, works like a charm. Thanks.

Comment by suman.sr...@gmail.com, Feb 7, 2012

Hi, thanks a ton! This works really well, particularly with Amazon Elastic Map Reduce - Amazon EMR (well, Hadoop in general) has the same restrictions as Google AppEngine? - its really hard to install custom libraries and have them working out of the box. I was able to use this for getting geo analysis on EMR ... will probably write up a tutorial on it soon as well.


Sign in to add a comment
Powered by Google Project Hosting