【发布时间】:2021-04-20 11:53:55
【问题描述】:
我想像国家代码一样检查用户的地理位置,这是我的代码:
from urllib.request import urlopen
url = 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)
country = data['country']
print(country)
当我在本地机器上运行它时,它会显示我当前的位置,但是当我在我的 ec2 服务器上运行这个脚本时,它会显示 ec2 本身的区域。
如何获取实际用户的位置,而不是 ec2 区域?
更新和解决方案
感谢 Mousetail,我需要在 URL 中使用 IP:
https://ipinfo.io/[the ip of the user]/json
【问题讨论】:
-
使用
https://ipinfo.io/[the ip of the user]/json -
@mousetail 谢谢!那行得通!非常感谢:)
标签: python python-3.x amazon-web-services flask amazon-ec2