Finding geolocation from an IP address

How can I find a location such as city/state or country from an IP address?

Is there a Ruby gem or Python module to do this? Or, is there a website which has an API?


Solution 1:

You could use the http://ipinfo.io API for this:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0838",
  "org": "AS15169 Google Inc.",
  "postal": "94040"
}

If you're only interested in one field you can add that to the URL to get it as plain text:

$ curl ipinfo.io/8.8.8.8/loc
37.3860,-122.0838

$ curl ipinfo.io/8.8.8.8/country
US

If you're looking for a python module https://github.com/juanpabloaj/pyipinfo is a simple wrapper, or http://geocoder.readthedocs.org/en/stable/ is more fully featured and has built in support for ipinfo.io and other providers.

Solution 2:

Yes, you can find the city, state, or country location of an IP address by using an IP geolocation API. I know of several websites offering such services to companies or curious individuals like you and me.

However, whatever’s available out there would have different features so you need to check them carefully before deciding on the software that would suit your requirements. With regards to the programming languages you mentioned, normally the best IP geolocation APIs support multiple programming languages including Ruby and Python.

You also specified details such as city, state, or country. Well, no matter which API you choose, those geo-data points would definitely be topmost in the output results whenever you make a query. But IP geolocation APIs usually provide more details such as time zone and postal code which are important if you wish to create a clearer picture of the IP address you are geolocating.

Here are the APIs that I personally use.

https://geoipify.whoisxmlapi.com/

This API helps find the geolocation of an IP address (check sample output here) from more than 9.5 million IP blocks in 250 countries. In my opinion, that number should be enough to include almost all of the known IP addresses now in use.

This provider also has a geolocation database that contains both IPv4 and IPv6 addresses with more than 340,000 locations in 120,000+ towns and cities worldwide. What I like is that it’s updated weekly and available for download in CSV or JSON format.

Before making a request, you will need a personal API key which you can get from the website’s ‘My Products’ page. To make a location search, you have the option to input either the IPv4 or IPv6 address, the domain name, or email address of your target.

The search results will contain your target’s country, region, city, latitude, longitude, postal code, time zone, ISP, the array of domains, autonomous system number, system name, API system route, and autonomous system website’s URL.

They are displayed in the default JSON format, although you have the choice to have it in either JSON or XML. System compatibility should not be a problem because the output supports 9 programming languages, including Ruby and Python.

In case you need to make multiple geolocation requests, there’s also an online bulk IP geolocation lookup service which claims to allow you to check up to 100,000 IP addresses in one go.

https:// https://geo.ipify.org/

The second provider I use claims to offer real-time geolocation from more than 8 million IP blocks from 250 countries.

They also have an IP geolocation database that contains almost 5 million accumulated records from almost all of the existing IP addresses (check sample output here). They update it monthly, and it is available for download in a relatively small file size of 40 Mb zipped.

Making a search request requires a personal API key which you can get from this provider’s subscription page. To make a location search, you need to input your target’s IPv4 address.

The query result shows the target’s precise location details including country, region, city, latitude, longitude, postal code, and time zone. The output can be displayed in either XML or JSON format.

The API also supports 9 programming languages — i.e., Javascript, C#, Java, PHP, NodeJS, Perl, PowerShell, as well as Python and Ruby.

Finding the city, state, or country location of an IP address is easy if you use an IP geolocation API. However, remember that your choice must be able to provide ample coverage and programming language compatibility to be able to give you what you are searching for. Geoipify.whoisxmlapi.com and Geo.Ipfy can definitely help in that regard.