Posts come very slowly, but here’s another one.
This post won’t be useful for most people :-(. You know when you use google analytics and get that fantastic map showing where people connect from? Read this, and you will learn how to do it yourself.
The code to do it is fairly easy, the interesting part is all the background.
Each computer connected to internet has its own IP address (we all know that already).
Either you will have an static address or a dynamic address. Static addresses are just like your day to day phone number, you have one and only one. Dynamic addresses are provided by ISPs and may be different on a day to day basis.
An IP by itself does not carry any type of information like phone numbers do. For example if I recieve a call from a number like:
From +34 I gather that it comes from Spain. More accurately I can say that it comes from somewhere in the Girona province, thanks to the 972.
Since we can’t obtain this information from the IP numbers, how do we get it?
We can ask the organizations that manage IPs in some way or another:
- Regional Registries like AfriNIC, ARIN, APNIC, LACNIC or RIPE NCC.
- Internet Service Providers.
At the core, is just a matter of having a good connection with key organizations to retrieve the newest data about IPs.
And we come back to one of the basic uses of computers, Databases.
No database to rule them all
We are not lucky. As you might expect, different companies got the idea to provide Geolocation databases based on IP. If there’s a need, there’s a market, that means that most of the good databases will cost you money.
As their information is based on mapping IPs to zones, we can’t expect a 100% accuracy, and there will always be some errors. Luckily at country level those errors are few. But we’ll get bigger errors when trying to pinpoint the city.
For this exercise we will use the GeoLite database provided by MaxMind. It comes in two flavours, one is plain text, the other is a binary database. The binary database will be faster to query.
Using GeoLite is a piece of cake as MaxMind already provides different APIs to access their databases.
MaxMind provides a Python API, but for this example we will use a very easy to use API called pygeoip.
It’s so easy that we can get the country from an IP address with only 4 lines of code!
import pygeoip gi = pygeoip.GeoIP('GeoIP.dat') country = gi.country_name_by_addr('184.108.40.206') print country
There you are. Now we can add this small piece of code anywhere. For example, if you run a python website (django anyone?) you could log where every connection comes from.
A lot of text and only three lines of code. 🙂 maybe I’m turning lazy.