Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get location of a user in Python Flask

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

like image 308
Nasrin Avatar asked Aug 24 '26 07:08

Nasrin


1 Answers

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

like image 174
Nasrin Avatar answered Aug 26 '26 23:08

Nasrin