gpt4 book ai didi

python - 解析 X-Forwarded-For 以在 Heroku 上使用 werkzeug 获取 ip

转载 作者:行者123 更新时间:2023-11-28 19:13:19 26 4
gpt4 key购买 nike

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

X-Forwarded-For的一般格式是:

X-Forwarded-For: client1, proxy1, proxy2

在 flask 上使用 werkzeug,我试图想出一个解决方案来访问客户端的原始 IP。

有谁知道这样做的好方法吗?

谢谢!

最佳答案

Werkzeug(和 Flask)将 header 存储在 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

关于python - 解析 X-Forwarded-For 以在 Heroku 上使用 werkzeug 获取 ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com