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.
Advertisement
Answer
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.
JavaScript
x
18
18
1
import json
2
import urllib.request
3
4
GEO_IP_API_URL = 'http://ip-api.com/json/'
5
6
# Can be also site URL like this : 'google.com'
7
IP_TO_SEARCH = '87.250.250.3'
8
9
# Creating request object to GeoLocation API
10
req = urllib.request.Request(GEO_IP_API_URL+IP_TO_SEARCH)
11
# Getting in response JSON
12
response = urllib.request.urlopen(req).read()
13
# Loading JSON from text to object
14
json_response = json.loads(response.decode('utf-8'))
15
16
# Print country
17
print(json_response['country'])
18