【问题标题】:How to get location of the user using Flask-Simple-GeoAPI [duplicate]如何使用 Flask-Simple-GeoAPI 获取用户的位置 [重复]
【发布时间】:2022-01-26 19:01:22
【问题描述】:

在我的项目中,我想访问用户的位置/城市,这样我就可以向用户位置附近的皮肤科医生展示,这只会在他们单击“访问我的位置”按钮时发生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

为什么它不显示我的国家、城市或任何内容? 如果可能,请帮忙!

【问题讨论】:

    标签: python html flask geolocation


    【解决方案1】:

    这是因为您发送的是本地主机地址 (Loopback / 127.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)
    

    【讨论】:

    • 如果我单独放置它就可以工作。但是,如果我部署我的网站,并且如果其他用户单击该按钮,我是否会在您提供的代码的帮助下了解他们的城市或国家/地区?
    • 是的,如果你有能力抓取传入请求的IP,你应该很好! kite.com/python/answers/…
    • 好的,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多