how to use GeoIP with Python

Sat Jul 28, 2012 3:27 pm

You may need to use GeoIP software for IP address geo location finding as free or paid service; GeoIP databases provide information until the Country/City level, the Maxmind’s GeoIP is a famous service for IP address geographic information among developers, there is API for Python language you can find it here:

This is a C Python library from Maxmind and you will need to run the following codes, now how to run the code? For sure we will import the GeoIP and we will use its API , following snippet we are trying to get city and country information:

import GeoIP

## creating an object
myGeoIP =

print myGeoIP
print myGeoIP.country_code_by_name("")

## Using GeoIP Memory Cache
print geo
print geo.country_code_by_name("")

## using the City database"/usr/local/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD)

print "Using GeoIP City database"
geoRecord = geo.record_by_addr("")

if geoRecord!= None:
    print geoRecord ['country_code']
    print geoRecord ['country_name']
    print geoRecord ['time_zone']
geoRecord ['city']
geoRecord ['region']
geoRecord ['region_name']
geoRecord ['postal_code']
geoRecord ['latitude']
geoRecord ['longitude']
geoRecord ['area_code']

You may wondering about this line :


GEOIP_MEMORY_CACHE: increases the performance but for sure needs more memory caching
GEOIP_CHECK_CACHE: a reload of the GEO database if the database file is modified

  Related Posts  to : how to use GeoIP with Python
 hashing in python     -  
 Reading email in Python     -  
 Python Module for MySQL     -  
 How to read and write to CSV file from python     -  
 exception handling try and catch in Python     -  
 usage of SQLite database from Python     -  
 Python Training from Certified Experts (     -  
 Build Linear Regression in Python - Supervised Learning     -  

Topic Tags

Python GEO