GeoIP is a very important addition to projects, moreover keeping the GeoIP databases up-to-date. I’ll be using pygeoip python API along with Maxmind binary databases to create a simple python script that returns country name according to the supplied IP address.
First of all to maintain an up-to-date version of the GeoIP databases from Maxmind, I wrote the following BASH script (maxmind.sh)
1 2 3 4 5 6 7 8 9 |
|
You could omit the GeoLiteCity and GeoIPASNum if not interested. This shell script will check the Maxmind website for a new version of the currently downloaded databases if any, then it extracts it into /usr/local/share/GeoIP/ to be accessed from the python script.
It’s propably best to create a crontab job to check updates every week or even daily, according to your requirements.
1
|
|
Then add the cronjob, for example to run it every week on Sundays I use the following command,
—make sure you have created /var/log/GeoIP.log
1
|
|
Now to the python script (geoip.py), which is based on pygeoip, it takes one argument “IP” and returns full version name of country + ISO shortcode.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Usage as follows,
1
|
|
That’s pretty much it.