如何使用Flask Simple GeoAPI获取用户的位置



在我的项目中,我想访问用户的位置/城市,这样我就可以向附近的皮肤科医生显示用户的位置,只有当他们单击html页面上的"访问我的位置"按钮时,这才会发生。因此,当用户单击此按钮时,@app.route((将转到app.py,我已经编写了以下代码。

@app.route('/access')
def my_location():
try:
geoip_data = simple_geoip.get_geoip_data()
return jsonify(data=geoip_data)
except Exception as e:
return render_template('home.html', msg = e)

但是当执行该代码时,[我得到以下结果。][1][1] :https://i.stack.imgur.com/wRCB0.png

为什么它没有显示我的国家、城市或其他什么?如果可能的话,请帮忙!

这是因为您正在发送本地主机地址(Loopback/120.0.0.0(。请尝试提供您的公共IP!

from simple_geoip import GeoIP
geoip = GeoIP("your-api-key");
try:
data = geoip.lookup("8.8.8.8") # Replace this with your public IP
except ConnectionError:
# If you get here, it means you were unable to reach the geoipify
# service, most likely because of a network error on your end.
except ServiceError:
# If you get here, it means geoipify is having issues, so the request
# couldn't be completed :(
except:
# Something else happened (non-geoipify) related. Maybe you hit CTRL-C
# while the program was running, the kernel is killing your process, or
# something else all together.
print(data)

最新更新