python - 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.
解决方案
Tornado 不支持在运行时更改处理程序。相反,制作您自己的处理程序,根据您想要的任何标准执行所需的重定向:
class MyRedirectHandler(RequestHandler):
def get(self):
self.redirect(self.settings['default_url'], permanent=False)
推荐阅读
- python - Pandas 遍历行并从另一列中删除一列中的字符串值
- python - 安装依赖项时出现 Python Pip 错误
- php - 如何设置自定义 Wordpress 主题以便能够在主题管理中更新它?
- html - CSS网格容器最小高度
- c# - 有没有办法动态生成代码以与 C# 中的按钮进行交互?
- c++ - C++ 入门 5 版。为 String 类编写移动构造函数
- javascript - 使用 ag-grid 进行单元格编辑的自定义日期选择器
- opencart - Opencart 搜索没有完整字符串的产品型号
- fullcalendar - 仅在 dayGridMonth 中显示描述
- python - 设备类型为 cuda 的预期对象,但获得了设备类型 cpu