简体   繁体   English

Python,Flask客户端IP地址

[英]Python, Flask client ip address

I need to log the IP address of every user of my webapp, that I've created with Python and Flask. 我需要记录我使用Python和Flask创建的Web应用程序的每个用户的IP地址。

I'm using 我正在使用

request.remote_addr

But that's return the IP address of the server the app is deployed to. 但这返回了将应用程序部署到的服务器的IP地址。 Any fixes to this? 有任何解决办法吗?

How do you deploy the flask application? 如何部署flask应用程序?

I guess you deploy your app via a reverse-proxy server like nginx, right? 我猜您是通过像nginx这样的反向代理服务器部署应用程序的,对吗?

If you did that then request.remote_addr is the address of your server because your server sent client's request to your application and sent the response to the client. 如果这样做了,那么request.remote_addr是服务器的地址,因为服务器将客户端的请求发送到了应用程序,并将响应发送到了客户端。

To fix this, see: http://flask.pocoo.org/docs/0.11/deploying/wsgi-standalone/#proxy-setups 要解决此问题,请参阅: http : //flask.pocoo.org/docs/0.11/deploying/wsgi-standalone/#proxy-setups

The easiest way to get the user's(also known as client) IP is to set this as a variable or use it directly. 获取用户(也称为客户端)IP的最简单方法是将其设置为变量或直接使用它。

request.environ['REMOTE_ADDR']

To get your server's IP: 要获取服务器的IP:

request.remote_addr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM