Amazon Elastic Beanstalk - get visitor IP address (python)

懵懂的女人 提交于 2021-02-10 18:16:15

问题


My application is developed in python and the bottle framework. I am using following code snippet to get IP address of visitors to the page:

user_ip = bottle.request.environ['REMOTE_ADDR']

It works fine on my local machine, however, after deployment to the AWS Beanstalk instance, I think I am getting the load balancer IP, as the user_ip reads something like 10.48.95.234.

Is my thinking correct? If so, is there any way to obtain real visitor's ip address?


回答1:


You are correct that the REMOTE_ADDR value you are getting is for the ELB.

You would typically need to look for the X-Forwarded-For header in the request. The ELB will insert this header to let you know the end client IP address.




回答2:


There is also a bottle-specific version that helps out in this case:

user_ip = bottle.request.remote_addr

This automagically does the processing and gets user's address. See documentation or source code for more details.




回答3:


This works for my ELB:

from bottle import route, run, request

@route('/hello')
def hello():
    return "Hello, {}".format(request.remote_route)

run(host="0.0.0.0", port=8000, server='twisted')


来源:https://stackoverflow.com/questions/14241704/amazon-elastic-beanstalk-get-visitor-ip-address-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!