-1

Output

Code:

ip_addr = request.environ['REMOTE_ADDR']
print(f'ipaddr --> {ip_addr}')
print(request.environ.get('HTTP_X_FORWARDED_FOR' , request.remote_addr))
print(f'Remote Addr --> {request.remote_addr}' )  

I try all this in order to get the client ip-addr but don't know why there locahost is coming.

Please Elaborate and guide.

davidism
  • 121,510
  • 29
  • 395
  • 339
muiz7
  • 1
  • 1
  • Are you sending a request from a terminal running in your computer to a server running in your computer? Because that's what `127.0.0.1` or `localhost` mean. If you make a request from your computer to `127.0.0.1`, the traffic doesn't go outside your computer. Therefore, the IP is "this computer" – Savir Apr 08 '22 at 22:01

1 Answers1

0

Your code is perfectly fine for getting client IP. Try to run your application with different any host (flask run -h 0.0.0.0) and access it from different machine on the same network, you should see it's IP then.

kosciej16
  • 6,294
  • 1
  • 18
  • 29