3

I want to check the user's geolocation like the country code, and this is my code:

from urllib.request import urlopen

url = 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)
country = data['country']
print(country)

when I run it in my local machine, it shows my current location, but when I run this script in my ec2 server, it shows the region of the ec2 itself.

how can I get the actual user's location, not the ec2 region?

Updated and Solution

Thanks to Mousetail, i need to use IP in the URL: https://ipinfo.io/[the ip of the user]/json

Nasrin
  • 157
  • 2
  • 9

2 Answers2

2

Thanks to Mousetail, I needed to use IP address in the URL: https://ipinfo.io/[the ip of the user]/json

Nasrin
  • 157
  • 2
  • 9
-1

It is showing your location because the request that is being carried out is executed server-side, not client-side. You could use maybe this to get the IP from the client-side in JavaScript.

Thanks