Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 609
Prasun Avatar asked Oct 16 '25 15:10

Prasun


1 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'])
like image 103
Aviv Yaniv Avatar answered Oct 19 '25 03:10

Aviv Yaniv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!