tornado获取客户端IP

网上搜到的用tornado获取客户端IP都使用self.request.headers['X-Real-Ip'],但实际翻tornado源代码发现只需要获取self.request.remote_ip即可取到客户端IP

见引用tornado源代码(对应文件为httpserver.py中HTTPRequest属性remote_ip)

if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get(
                "X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream, 
                                           iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"

Via:Chairo@2011年09月29日-EOF-

添加新评论 »

captcha 请输入验证码