Return to
David's Python Resources
ip2country
A python module for looking up the country in which a given IP address resides.
Features/quirks:
Maintains a cache of IP address allocations, downloading the table from the APNIC database only if the cached version is missing or stale
Fast lookup - builds optimal lookup tables from the cached APNIC file
Ideal for situations where you want to do frequent lookups within your program
Just run the file under Python to get an interactive demo
Download
download
ip2country.tar.gz
Installation
Feel free to tweak the constants at the top of the file before you install it
become root, and type 'python setup.py install', OR
just copy the file into your python modules load path
Usage
There's a simple demo at the bottom of the file, which you can invoke by simply running the file with the python interpreter
Here's an example:
>>> import ip2country
>>> ip2c = ip2country.IP2Country(verbose=1)
>>> ip2c.lookup("202.27.184.3")