我有一个在 AWS Elastic Beanstalk 上运行的 Python/Flask 应用程序。
当 Python 脚本被触发时,我需要获取用户的远程 IP 地址,但在 python 脚本中使用 request.environ['REMOTE_ADDR'] 不起作用。它返回的似乎是我的服务器的私有(private) IP 地址,而不是用户的 IP 地址。
http://xxxxxx-env.eu-west-2.elasticbeanstalk.com/xxx
xxx 解析为我的 Python 应用程序,其中包含:
if ('REMOTE_ADDR' in request.headers):
print (request.environ['REMOTE_ADDR'])
但是结果是: 172.31.xx.xxx
当我的实际IP是:
83.164.xx.xxx
有人能告诉我如何获取用户的远程 IP 的正确方向吗?
最佳答案
我设法解决了这个问题。 Elastic Beanstalk 自动传递远程 IP 地址,并且可以按如下方式检索它:
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
关于javascript - AWS 上的 Python 获取远程 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46542463/