解析X-Forwarded - 在Heroku上用werkzeug获得IP [英] Parse X-Forwarded-For to get ip with werkzeug on Heroku
问题描述
一般的格式X-Forwarded-For是:
X-Forwarded-For:client1,proxy1,proxy2
在烧瓶上使用werkzeug,我试图想出一个解决方案来访问客户端的原始IP地址。
有没有人知道这样做的好方法?
谢谢!
werkzeug.datastructures.Headers
。你应该可以这样做: provided_ips = request.headers.getlist(X-Forwarded-For)
#列表中的第一个条目应该是客户的IP。
或者,您可以使用 request.access_route
(感谢@Bastian指出!):
provided_ips = request.access_route
pre>
#列表中的第一项是客户的IP
Heroku proxies requests from a client to server, so you have to parse the X-Forwarded-For to find the originating IP address.
The general format of the X-Forwarded-For is:
X-Forwarded-For: client1, proxy1, proxy2
Using werkzeug on flask, I'm trying to come up with a solution in order to access the originating IP of the client.
Does anyone know a good way to do this?
Thank you!
解决方案Werkzeug (and Flask) store headers in an instance of
werkzeug.datastructures.Headers
. You should be able to do something like this:provided_ips = request.headers.getlist("X-Forwarded-For") # The first entry in the list should be the client's IP.
Alternately, you could use
request.access_route
(thanks @Bastian for pointing that out!):provided_ips = request.access_route # First entry in the list is the client's IP
这篇关于解析X-Forwarded - 在Heroku上用werkzeug获得IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!