How to get client ip address in flask

Flask have 3 methods to retrive client IP

1, request.remote_addr

2, request.environ[‘REMOTE_ADDR’]

3, request.environ[‘HTTP_X_FORWARDED_FOR’]

For the first 2 methods request.remote_addr and request.environ['REMOTE_ADDR'], we can use it if the server was directly called by clients.

But if our sevice behind a proxy, we need to look for the “X-Forwarded-For” header for the client IP. Which means we have to use the 3rd method request.environ['HTTP_X_FORWARDED_FOR'] to get the client IP.

Here is the details of the X-Forwarded-For

The X-Forwarded-For (XFF) HTTP header field is a common method for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer.

End;

Leave a Reply

Your email address will not be published. Required fields are marked *