解析X-Forwarded-For以在Heroku上使用werkzeug获得IP
问题内容:
Heroku代理从客户端到服务器的请求,因此您必须解析X-Forwarded-For才能找到原始IP地址。
X-Forwarded-For的一般格式为:
X-Forwarded-For: client1, proxy1, proxy2
我在烧瓶上使用werkzeug,我试图提出一种解决方案,以便访问客户端的原始IP。
有人知道这样做的好方法吗?
谢谢!
问题答案:
Werkzeug(和Flask)将标头存储在的实例中werkzeug.datastructures.Headers
。您应该能够执行以下操作:
provided_ips = request.headers.getlist("X-Forwarded-For")
# The first entry in the list should be the client's IP.
或者,您可以使用request.access_route
(感谢@Bastian指出这一点!):
provided_ips = request.access_route
# First entry in the list is the client's IP