问题
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