Python, Flask client ip address

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?

Solved

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


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


Comments