首页 > 解决方案 > How to replace or modify a Tornado Handler at runtime?

问题描述

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.

标签: pythontornado

解决方案


Tornado 不支持在运行时更改处理程序。相反,制作您自己的处理程序,根据您想要的任何标准执行所需的重定向:

class MyRedirectHandler(RequestHandler):
    def get(self):
        self.redirect(self.settings['default_url'], permanent=False)

推荐阅读