Amazon EC2 doesn’t allow easy installation of a “python-geoip” for GeoIP usage with Python, so you have to do this manually.First, we need to install the GeoIP library itself – Amazon EC2 allows for this:
sudo yum -y install GeoIP
But before you can do anything, you need to install gcc and python-devel packages to compile and build this:
sudo yum -y install gcc python-devel GeoIP-devel
Then download and install the latest version of the Python code from Maxmind:
wget http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.7.tar.gz tar xzvf GeoIP-Python-1.2.7.tar.gz cd GeoIP-Python-1.2.7
Then build and install the python GeoIP libraries.
python setup.py build sudo python setup.py install
That’s it! You should have a working Python+GeoIP package in your system! You can test it out as follows:
Now, if you want the city-level precision working, you will need to download the city data from Maxmind:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip GeoLiteCity.dat.gz sudo mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
And then you can run the city-level geo-targeting.
Note: you may need to edit the GeoIP file open line in test_city.py as follows, since that is where the GeoIPCity file is at:
gi = GeoIP.open("/usr/share/GeoIP/GeoIPCity.dat",GeoIP.GEOIP_STANDARD)
(Updated Feb 6, 2012 to reflect correct python-devel package name, and -y option to remove yum install prompt.)