How to get the Country according to a certain IP? [duplicate]

Does anyone know any simple way to retrieve the country from a given IP Address, preferably in ISO_3166-1 format?


There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building.

For services:

  • http://www.hostip.info/use.html (as mentioned by Mark)
  • http://www.team-cymru.org/Services/ip-to-asn.html

For lists:

  • http://www.maxmind.com/app/geoip_country (as mentioned by Orion)
  • You could roll your own by downloading the lists from the RIRs:

    • ftp.arin.net/pub/stats/arin/delegated-arin-latest
    • ftp.ripe.net/ripe/stats/delegated-ripencc-latest
    • ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest
    • ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest
    • ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest

      The format is documented in this README


A lot of people (including my company) seem to use MaxMind GeoIP.

They have a free version GeoLite which is not as accurate as the paid version, but if you're just after something simple, it may be good enough.


Here's a nice free service with a public API: http://www.hostip.info/use.html


ipinfodb provides a free database and API for IP to country and vice versa. They use free data from MaxMind. The data gets updated every month, and it's a great free alternative with decent accuracy.


I don't know how accurate http://hostip.info site is. I just visited that site, and it reported that my country is Canada. I'm in the US and the ISP that my office uses only operates from the US. It does allow you to correct its guess, but if you are using this service to track web site visitors by country, you'll have no way of knowing if the data is correct. Of course, I'm just one data point. I downloaded the GeoLite Country database, which is just a .csv file, and my IP address was correctly identified as US.

Another benefit of the MaxMind product line (paid or free) is that you have the data, you don't incur the performance hit of making a web service call to another system.