简体   繁体   English

如何获取到Heroku app的请求地址IP?

[英]How to get the IP address of the request to a Heroku app?

Heroku has a routing system to forward requests to the dynos. Heroku 有一个路由系统将请求转发给测功机。 My application needs to know from where the request came, but it always gets random addresses in a.network, probably Heroku's internals.我的应用程序需要知道请求来自何处,但它总是在 a.network 中获取随机地址,可能是 Heroku 的内部结构。

And I see that in the logs, it (Heroku's router) gets my IP address and forwards the request.我在日志中看到,它(Heroku 的路由器)获取了我的 IP 地址并转发了请求。 Is there a way to get the actual IP address of a request?有没有办法获取请求的实际 IP 地址?

My application is written in Python, using Flask我的应用写在Python,使用Flask

Checking Flask's documentation on filtering headers etc., I found that: 查看Flask关于过滤标头等的文档,我发现:

request.headers['X-Forwarded-For']

is where you'll get the client's real IP address. 是您获取客户端的真实IP地址的地方。


From a deleted comment by OP, this article provides a safer solution . 从OP删除的评论中,本文提供了更安全的解决方案

You want to preserve the IP from request.remote_addr when locally or if hosting the site somewhere else:您想在本地或在其他地方托管网站时从request.remote_addr保留 IP:

def getIP():
    if 'X-Forwarded-For' in request.headers:
        return request.headers['X-Forwarded-For']
    return request.remote_addr

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

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