Search code examples
pythontornado

Retrieve browser headers in Python


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 is Tornado's alternative?

Note: How do I get the client IP of a Tornado request? and the "request" does not work for me.


Solution

  • Here's a snippet based off of a server I have where we retrieve some header data from the request:

    class api(tornado.web.RequestHandler):
        def initialize(self, *args, **kwargs):
            self.remote_ip = self.request.headers.get('X-Forwarded-For', self.request.headers.get('X-Real-Ip', self.request.remote_ip))
            self.using_ssl = (self.request.headers.get('X-Scheme', 'http') == 'https')
        def get(self):
            self.write("Hello " + ("s" if self.using_ssl else "") + " " + self.remote_ip)