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?

7 Answers 7


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

$ curl ipinfo.io/
  "ip": "",
  "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/

$ curl ipinfo.io/

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.


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.


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.


You can go here...Any such service is only a rough estimate for ANY IP address, but this is what you are looking for.

http://www.geobytes.com/iplocator.htm (just enter the "IP Address to locate" and hit submit)

  • 1
    I have a long list of ip addresses so I was hoping I would be able to do it through a script
    – ed1t
    Jun 18, 2011 at 18:45

If you want to see a geolocation from a website you are navigating easily, you should install Flagfox add-on for Firefox: you will se a flag near address bar and clicking, on it, more information (city, IP...).

Also, you can use http://geo.flagfox.net to see the same detais for any IP address or server name.


Hostip.info is an open-source project with the goal to build/maintain a database mapping IP addresses to cities. Their about page explains the data sources relied on to populate this database.

Using HostIP, there are two ways to get location data from an IP address:

They also have a well-designed and easy-to-use RESTFUL API: just pass in your ip address after the ip= in the GET request string):

import urllib

response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=').read()


Second, the Project Website also makes its complete database available for download.


you may use geoiplookup from GeoIP package

geoiplookup -i

GeoIP Country Edition: PT, Portugal
range_by_ip: -
network: - ::16
ipnum: 1486725146
range_by_num: 1486684160 - 1486749695
network num:  1486684160 - 1486749695 ::16
GeoIP City Edition, Rev 1: PT, 14, Lisboa, Lisbon, 1350-008, 38.716702, -9.133300, 0, 0
range_by_ip: -
network: - ::23
ipnum: 1486725146
range_by_num: 1486725120 - 1486725887
network num:  1486725120 - 1486725631 ::23
GeoIP ASNum Edition: AS2860 Nos Comunicacoes, S.A.
range_by_ip: -
network: - ::18
ipnum: 1486725146
range_by_num: 1486684160 - 1486736383
network num:  1486716928 - 1486733311 ::18

rpm -qa | grep -iP "geoip|geolite"


Verify proxy exit location without installing anything

I needed this to confirm tor reads updated exit node setting.

curl ipinfo.io/"$(curl -s --proxy socks5://localhost:9050 ifconfig.me)"

Had to do two requests because the limit at ipinfo.io was exceeded for this exit node. Looks like a handy alias, if you think about it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .