Skip to content Skip to sidebar Skip to footer

How Can I Get The User's Ip-address In My Cloud-run Flask App?

I have a Flask app running via Google Cloud Run and I need to know the user's IP-Address. I am using gunicorn as my Server. I have tried the following code: request.remote_addr req

Solution 1:

You have headers provided by Google. In my tests I got these 2:

X-Forwarded-For: [MyPubliCIp, LoadBalancerIp,MyPubliCIp]Forwarded: [for="MyPubliCIp";proto=http]

Use them as you want.

Solution 2:

In Cloud Run, inside a Flask route I've done this:

ip = request.environ.get("HTTP_X_FORWARDED_FOR", request.remote_addr)
logging.info(f"IP ADDRESS: {ip}")

With this I can get the requester public IP address.

request.remote_addr works in servers like App Engine.

Post a Comment for "How Can I Get The User's Ip-address In My Cloud-run Flask App?"