解析X-Forwarded - 在Heroku上用werkzeug获得IP [英] Parse X-Forwarded-For to get ip with werkzeug on Heroku

查看:205
本文介绍了解析X-Forwarded - 在Heroku上用werkzeug获得IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heroku代理从客户端到服务器的请求,所以你必须解析X-Forwarded-For找到原始IP地址。



一般的格式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 
#列表中的第一项是客户的IP
pre>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆