4

I need to log the IP address of every user of my webapp, that I've created with Python and Flask.

I'm using

request.remote_addr

But that's return the IP address of the server the app is deployed to. Any fixes to this?

Brian
  • 13,996
  • 19
  • 70
  • 94
  • 1
    Possible duplicate of [Get IP address of visitors using Python + Flask](http://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-python-flask) – javad Nov 02 '16 at 09:52

2 Answers2

5

How do you deploy the flask application?

I guess you deploy your app via a reverse-proxy server like nginx, right?

If you did that then request.remote_addr is the address of your server because your server sent client's request to your application and sent the response to the client.

To fix this, see: http://flask.pocoo.org/docs/0.11/deploying/wsgi-standalone/#proxy-setups

JJC
  • 9,547
  • 8
  • 48
  • 53
THaGKI9
  • 86
  • 4
-2

The easiest way to get the user's(also known as client) IP is to set this as a variable or use it directly.

request.environ['REMOTE_ADDR']

To get your server's IP:

request.remote_addr

Sanjay
  • 203
  • 1
  • 4
  • 10
  • `request.environ['REMOTE_ADDR']` and `request.remote_addr` are the same thing (and not solving the problem the OP asked about.) – Arel Jul 10 '20 at 04:56