Python, Flask client ip address

12,974

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

Share:
12,974

Related videos on Youtube

Brian
Author by

Brian

Updated on June 04, 2022

Comments

  • Brian
    Brian almost 2 years

    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
    Brian over 7 years
    I deploy the app with supervisor on AWS
  • Brian
    Brian over 7 years
    Actually, I found out that this request.headers.get('X-Forwarded-For', request.remote_addr) works well for my needs
  • Arel
    Arel almost 4 years
    request.environ['REMOTE_ADDR'] and request.remote_addr are the same thing (and not solving the problem the OP asked about.)