N.Widds
N.Widds

Reputation: 277

Python On AWS Get Remote IP Address

I have a Python/Flask app running on the AWS Elastic Beanstalk.

I need to get the remote IP address of the user as the Python script is triggered but using request.environ['REMOTE_ADDR'] in the python script isn't working. It's returning what looks to be the private IP address of my server rather that the IP address of the user.

http://xxxxxx-env.eu-west-2.elasticbeanstalk.com/xxx

xxx resolves to my Python app containing:

if ('REMOTE_ADDR' in request.headers):
    print (request.environ['REMOTE_ADDR'])

But the result is: 172.31.xx.xxx

When my actual IP is:

83.164.xx.xxx

Can anybody point me in the right direction as to how to get the user's remote IP?

Upvotes: 2

Views: 682

Answers (1)

N.Widds
N.Widds

Reputation: 277

I managed to work this out. Elastic Beanstalk automatically passes the Remote IP Address and it can be retrieved as follows:

if (request.access_route):
    #return remote ip
    remoteIP = request.access_route[0]
    #return full ip list (if more than one passed through)
    remoteIPRaw = request.access_route

Upvotes: 3

Related Questions