1

In my project, I want to access the location/city of the user so that I’ll be able to show the dermatologists nearby the users location and this will only happen when they clicks the button “Access my location” on the html page. So when the user clicks this button, the @app.route() goes to the app.py and I have written the following code.

@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)

But when this code is executed, [I get the following result.][1] [1]: https://i.stack.imgur.com/wRCB0.png

Why doesn't it show my country, city or anything? Please help if possible!

1 Answers1

1

Its because you are sending the localhost address (Loopback / 127.0.0.0). Try providing your public IP instead!

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)
  • 1
    Its working if I put individually. But will this work if I deploy my site and if other users click on the button, will I get to know their city or country with the help of the code that you've provided? – Dilrose Reji Jan 26 '22 at 11:09
  • 1
    Yes, if you have the capability to grab the IP of the incoming request, you should be good! https://www.kite.com/python/answers/how-to-get-an-ip-address-using-flask-in-python – EvilDeveloper Jan 26 '22 at 11:14
  • Alright, thank you! – Dilrose Reji Jan 26 '22 at 14:02