How to get location of server based on IP address in Python

Viewed 4408

Can we program (probably in python) to get the server location from where we are accessing the webpage.

P.S. Its just for fun and to get in-depth knowledge.

5 Answers

This is an example of fetching server location using one of the APIs.

There are many others site options such as keycdn, iplocation and more.

import json
import urllib.request

GEO_IP_API_URL  = 'http://ip-api.com/json/'

# Can be also site URL like this : 'google.com'
IP_TO_SEARCH    = '87.250.250.3'

# Creating request object to GeoLocation API
req             = urllib.request.Request(GEO_IP_API_URL+IP_TO_SEARCH)
# Getting in response JSON
response        = urllib.request.urlopen(req).read()
# Loading JSON from text to object
json_response   = json.loads(response.decode('utf-8'))

# Print country
print(json_response['country'])

You could use GeoIP tools like this to get the location from the IP address. It's not 100% accurate but usually it's close enough.

There are also python modules such as GeoIP and python-geoip which do basically what you want. If you want the most accurate results you need to buy more accurate database from MaxMind.

You can use some API service for that.

For example, https://apiip.net , their API provides a lot of info about the IP, also you can request XML responses if needed.

Simple GET call:

https://apiip.net/api/check?ip=67.250.186.196&accessKey={your_api_key}

To get the location of an IP address you can use IPinfo.io. You get up to 50,000 requests/month on the free tier and you can get the following geolocation information from the API by looking up an IP address:

  • city
  • region
  • country
  • loc (geolocation coordinates: latitude & longitude)
  • postal code
  • timezone

You can use the IPinfo API or the IPinfo Python module to get started. The Python Module is pretty easy to use.

# 1. make sure you have installed the `ipinfo` module with `pip install ipinfo`
import ipinfo 

# 2. you can get your Token from your account dashboard.
access_token = 'insert_token_here'


# 3. initialize the handler which you will use to make query
handler = ipinfo.getHandler(access_token)

# 4. replace this with the IP address you want to lookup
ip_address = '8.8.8.8'


# 5. lookup the IP address for it's geolocation info
details = handler.getDetails(ip_address)

print(details.city) # the city where the IP address is located
print(details.region) # the region where the IP address is located
print(details.country) # the country where the IP address is located
print(details.loc) # this will print the latitude and longitude of the IP address

# or you can print out a dictionary that contains all the details
print(details.all)

You can use bin file to lookup the IP address information. For instance, using IP2Location Python library and IP2Location Lite bin file:

import IP2Location
 
IP2LocObj = IP2Location.IP2Location()

IP2LocObj.open("IP2LOCATION-LITE-DB11.BIN")
rec = IP2LocObj.get_all("8.8.8.8")
 
print rec.country_short
print rec.country_long
print rec.region
print rec.city
print rec.isp
print rec.latitude
print rec.longitude
print rec.zipcode
print rec.timezone

You can get the IP2Location Lite database from here: https://lite.ip2location.com/ip2location-lite

Related