Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace or modify a Tornado Handler at runtime?

Tags:

python

tornado

I am writing a Jupyter server extension, allowing me to write a tornado.web.RequestHandler class. I would like to modify one of the handlers that the application has been initialized with, specifically the one creating a default redirect:

(r'/?', web.RedirectHandler, {
    'url' : settings['default_url'],
    'permanent': False, # want 302, not 301
})

From the RequestHandler object I have access to the tornado.web.Application subclass used. Is there a public API to get the list of handlers that I could modify?

Specifically, I'm looking to change the 'url' parameter the tornado.web.RedirectHandler is created with. It doesn't look like there is a documented api for this, so I'm guessing I'd have to replace the handler entirely.

like image 211
sargas Avatar asked Jun 21 '26 13:06

sargas


1 Answers

Tornado does not support changing handlers at runtime. Instead, make your own handler which does the desired redirect based on whatever criteria you want:

class MyRedirectHandler(RequestHandler):
    def get(self):
        self.redirect(self.settings['default_url'], permanent=False)
like image 55
Ben Darnell Avatar answered Jun 23 '26 03:06

Ben Darnell