2

I saw on this thread on StackOverflow: How to find location with IP address in Python?

The answer worked for me, but so far I have only tested it on localhost, and I want to make sure, that the IP-address is actually the IP from the user, and not the server itself.

The reason I'm in doubt because I tried to different methods to test this out

 #I'm using a Mail API, but here I will print for simplicity
'IP-address from user ' + request.remote_addr #retures 127.0.0.1 when running on localhost

whenever I try with the ipinfo API, I get the actual IP-address from my internet connection, not from localhost.

url= 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)

IP=data['ip']
org=data['org']
city = data['city']
country=data['country']
region=data['region']  

 print('IP : {4} \nRegion : {1} \nCountry : {2} \nCity : {3} \nOrg : {0}'.format(org,region,country,city,IP))
#returns actual IP-address from my internet connection

How can I ensure, that the later solution actual returns the IP and location from the user, the accesses the service

Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69

1 Answers1

2

request.remote_addr gives you the address from which the request came. If you're running this on your localhost, then yes, the request will also be coming from localhost, so that's the IP you get.

If your server is set up in some specific proxying configuration, so all your server is seeing of incoming requests is the IP of the proxy, then you should know that that's the case (because you are setting up the server that way) and ensure that the proxy is passing the visitor's IP in some other fashion (i.e. HTTP headers) and specifically use that instead of request.remote_addr. Again, that is something you will know about if and when you're in that situation.

When you're making a request to ipinfo.io from your server, then ipinfo.io will see the address of your server as the originator of the request and use that as the IP address you're asking about. Since the request goes over the internet, it sees your public internet address. If you want your server to query about the IP of your visitor, then your server will need to include the visitor's IP in the request, otherwise ipinfo.io has no chance of knowing that IP. The request then needs to look like:

url = 'http://ipinfo.io/' + request.remote_addr
deceze
  • 510,633
  • 85
  • 743
  • 889
  • That's a valid point, thank you for your answer... However, whenever I try this I get an HTTP error 404. I don't think I can pass the IP-address directly into the URL. – Kristoffer Tølbøll Aug 27 '18 at 09:36
  • HTTPError: HTTP Error 404: Not Found – Kristoffer Tølbøll Aug 27 '18 at 09:36
  • Well, that's what the documentation says: https://ipinfo.io/developers#ip-address-parameter. What URL are you trying to request? And perhaps ipinfo.io/127.0.0.1 will indeed yield a 404, since there can be no info on that address. – deceze Aug 27 '18 at 09:38
  • yes right now I'm just running localhost, so that may very well be the case, thank you for pointing that out. I could deploy it to a test server, to see if it works with an acutal IP? I just want to make sure, that you can just add the request.remote_addr parameter to the url? – Kristoffer Tølbøll Aug 27 '18 at 09:46
  • In general: yes. See possible caveats in my answer… – deceze Aug 27 '18 at 09:47
  • Thank you.. do you know if it is still possible to get the answer as a json object? by adding /json to the url? – Kristoffer Tølbøll Aug 27 '18 at 09:49
  • 1
    That's the default anyway as far as I can see…!? Either way, read their documentation. – deceze Aug 27 '18 at 09:52