,

Python – GeoIP City and Country using MaxMind

PyGeoIP is a pure python GeoIP API based on MaxMind’s C-based Python API.
The python implementation is ported from the PHP GeoIP API written by Jim Winstead and Hans Lellelid.

Option 1: Installation using PIP


sudo pip install pygeoip

Option 2: Download and Install from Github repository

An alternative to pip is to download the code from github and then run the installation script.

# install or update the GIT client
sudo apt-get install -y git-core

# create download and build directories
mkdir -p ~/build/pygeoip
cd ~/build/pygeoip

# clone repository
git clone https://github.com/appliedsec/pygeoip

# install the library
cd pygeoip
sudo python setup.py install

Option 3: MaxMind – libGeoIP Python Library

MaxMind also offers a Python library that links against the libGeoIP C library.


mkdir -p ~/build/pygeoipmax
cd ~/build/pygeoipmax
wget http://www.maxmind.com/download/geoip/api/python/GeoIP-Python-latest.tar.gz
tar xvfz GeoIP-Python-latest.tar.gz
cd GeoIP-Python-*/
python setup.py build
sudo python setup.py install

Prepare Test

# create directories to test GeoIP
mkdir -p ~/build/geoip
cd  ~/build/geoip

# Download GeoIP databases
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
tar vxvf GeoLiteCity.dat.gz

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
tar vxvf GeoIP.dat.gz

pygeoip_test.py

# -*- coding: UTF-8 -*-

import pygeoip

def main():
    geoip_country()
    geoip_city()
    print 'done'

def geoip_city():
    path = './GeoLiteCity.dat'
    gic = pygeoip.GeoIP(path)
    print gic
    print 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'}
    print 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'}
    print gic.region_by_name('google.com')
    #{'region_name': 'CA', 'country_code': 'US'}
    print gic.region_by_addr('64.233.161.99')
    #{'region_name': 'CA', 'country_code': 'US'}

def geoip_country():
    path = './GeoIP.dat'
    gi = pygeoip.GeoIP(path)
    #print gi.country_code_by_name('google.com')
    #'US'
    print gi.country_code_by_addr('64.233.161.99')
    #'US'
    #print gi.country_name_by_name('google.com')
    #'United States'
    print gi.country_name_by_addr('64.233.161.99')
    #'United States'

if __name__ == '__main__':
    main()

References

1. MaxMind Home: http://www.maxmind.com
2. MaxMind GeoIP Database Download: http://geolite.maxmind.com/download/geoip/database/
3. MaxMind Python Library: http://dev.maxmind.com/geoip/downloadable#Python-5
4. PyGeoIP Installation: http://code.google.com/p/pygeoip/wiki/Install
5. PyGeoIP Usage Guide: http://code.google.com/p/pygeoip/wiki/Usage
6. Python GeoIP (python-geoip) Cities Tutorial: http://pointlessrants.com/post/python-geoip-python-geoip-cities-tutorial-
7. GeoIP Country City Python Tutorial: http://www.ip2location.com/python.aspx
9. Easy IP-to-country lookup in Python: http://blog.brush.co.nz/2009/07/geoip/


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *