使用 Flask for Python 获取访问者的 IP 地址
我正在制作一个网站,用户可以使用 Flask 微框架(基于Werkzeug)它使用Python(在我的例子中是2.6)。
我需要在用户登录时获取他们的 IP 地址(用于记录目的)。 有谁知道该怎么做?当然有办法用 Python 来做到这一点吗?
I'm making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).
I need to get the IP address of users when they log on (for logging purposes).
Does anyone know how to do this? Surely there is a way to do it with Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
请参阅有关如何访问 Request 对象的文档,然后从同样的 Request 对象,属性
remote_addr
。代码示例
有关更多信息,请参阅Werkzeug 文档。
See the documentation on how to access the Request object and then get from this same Request object, the attribute
remote_addr
.Code example
For more information see the Werkzeug documentation.
代理可能会让这件事变得有点棘手,请务必查看 ProxyFix(Flask 文档)如果您正在使用一个。查看您特定环境中的
request.environ
。对于 nginx,我有时会做这样的事情:当代理(例如 nginx)转发地址时,它们通常会在请求标头中的某个位置包含原始 IP。
更新 查看flask-security实施。再次强调,在实施之前请查看有关 ProxyFix 的文档。您的解决方案可能会根据您的特定环境而有所不同。
Proxies can make this a little tricky, make sure to check out ProxyFix (Flask docs) if you are using one. Take a look at
request.environ
in your particular environment. With nginx I will sometimes do something like this:When proxies, such as nginx, forward addresses, they typically include the original IP somewhere in the request headers.
Update See the flask-security implementation. Again, review the documentation about ProxyFix before implementing. Your solution may vary based on your particular environment.
实际上,您会发现,只需获取以下内容即可获得服务器的地址:
如果您想要客户端 IP 地址,请使用以下内容:
Actually, what you will find is that when simply getting the following will get you the server's address:
If you want the clients IP address, then use the following:
下面的代码始终给出客户端的公共 IP(而不是代理后面的私有 IP)。
The below code always gives the public IP of the client (and not a private IP behind a proxy).
我有 Nginx 和以下 Nginx 配置:
@tirtha-r< /a> 解决方案对我有用
我的请求和响应:
I have Nginx and With below Nginx Config:
@tirtha-r solution worked for me
My Request and Response:
可以使用以下代码片段检索用户的 IP 地址:
The user's IP address can be retrieved using the following snippet:
httpbin.org 使用此方法:
httpbin.org uses this method:
如果您在其他平衡器(例如 AWS Application Balancer)后面使用 Nginx,则 HTTP_X_FORWARDED_FOR 返回地址列表。可以这样修复:
If you use Nginx behind other balancer, for instance AWS Application Balancer, HTTP_X_FORWARDED_FOR returns list of addresses. It can be fixed like that:
这是最简单的解决方案,以及如何在生产中使用。
将
include proxy_params
添加到/etc/nginx/sites-available/$project
。include proxy_params
转发由 ProxyFix 解析的以下标头。Here is the simplest solution, and how to use in production.
Add
include proxy_params
to/etc/nginx/sites-available/$project
.include proxy_params
forwards the following headers which are parsed by ProxyFix.如果您使用 Gunicorn 和 Nginx 环境,那么以下代码模板适合您。
If You are using Gunicorn and Nginx environment then the following code template works for you.
这应该可以完成工作。
它提供客户端 IP 地址(远程主机)。
请注意,此代码在服务器端运行。
This should do the job.
It provides the client IP address (remote host).
Note that this code is running on the server side.
我没有通过 Google Cloud App Engine 获得上述任何工作。这有效,但是
建议的
request.remote_addr
每次都只返回本地主机 IP。I did not get any of the above work with Google Cloud App Engine. This worked, however
The proposed
request.remote_addr
did only return local host ip every time.如果使用 Cloudflare Argo 隧道,请使用:
If using Cloudflare Argo Tunnels, use: