Home > Uncategorized > MaxMind GeoIP python API

MaxMind GeoIP python API

It may be appealing to have the MaxMind GeoIP databases available to you for a variety of reasons.

Updating the databases:

echo "wget http://www.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz" > /usr/local/share/GeoIP/getgeoipdbs.sh
echo "gunzip -df ./GeoIPASNum.dat.gz" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "mv -f ./GeoIPASNum.dat /usr/local/share/GeoIP/" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "gunzip -df ./GeoIP.dat.gz" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "mv -f ./GeoIP.dat /usr/local/share/GeoIP/" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "gunzip -df ./GeoLiteCity.dat.gz" >> /usr/local/share/GeoIP/getgeoipdbs.sh
echo "mv -f ./GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat" >> /usr/local/share/GeoIP/getgeoipdbs.sh
chmod r+x /usr/local/share/GeoIP/getgeoipdbs.sh
/usr/local/share/GeoIP/getgeoipdbs.sh

Create a cron job:

crontab -e
#cron table entry as (without the ##)
## 0 0 1 * * bash /usr/local/share/GeoIP/getgeoipdbs.sh

Downloading, build and install the python API:

cd
wget http://www.maxmind.com/download/geoip/api/c/GeoIP-latest.tar.gz
tar zxvf GeoIP-latest.tar.gz
cd GeoIP-*
./configure && make && make install
cp ./apps/geoipupdate-pureperl.pl /usr/local/bin/
wget http://www.maxmind.com/download/geoip/api/python/GeoIP-Python-latest.tar.gz
tar zxvf GeoIP-Python-latest.tar.gz
cd GeoIP-Python-*/
python setup.py build
echo /usr/local/lib > /etc/ld.so.conf.d/local_libs.conf

Test the API:

python test.py

I ran an `strace` on test.py and it seems to access `/usr/local/share/GeoIP/GeoIP.dat`, which is the “GeoLiteCountry” database, which can be assumed to be the default when creating an object with GeoIP.new(GeoIP.[read database to here]).

However, test.py and the other examples contain definitions for GeoIP.open() that open other databases, so that you can more easily define any of the above databases for reference.

The python API is very straightforward and the README/docs are very small. The C docs contain real information about the core C class (looks like the py API is just a wrapper).

Advertisements
Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: