Retrieve browser headers in Python

前端 未结 2 1927
时光取名叫无心
时光取名叫无心 2021-02-05 17:57

I\'m currently drawing a blank as how to get the current browser header information for a user in Python Tornado? For example, in PHP you\'d simple view the $_SERVER data. What

2条回答
  •  清歌不尽
    2021-02-05 18:53

    You can use logic similar to tornado/httpserver.py or just create tornado.httpserver.HTTPServer() with xheaders=True.

    # Squid uses X-Forwarded-For, others use X-Real-Ip
    ip = self.headers.get("X-Forwarded-For", self.remote_ip)
    ip = ip.split(',')[-1].strip()
    ip = self.headers.get(
        "X-Real-Ip", ip)
    if netutil.is_valid_ip(ip):
        self.remote_ip = ip
    # AWS uses X-Forwarded-Proto
    proto = self.headers.get(
        "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
    if proto in ("http", "https"):
        self.protocol = proto
    

提交回复
热议问题