python - 从 Tornado Python 中的 RequestHandler 访问 URL

标签 python class handler tornado multiple-inheritance

在我的 super 简单 Tornado URL 调度程序中:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Main MainHandler ")

class MainHandler1(tornado.web.RequestHandler):
    def get(self):
        self.write("Main MainHandler 1")

class api_v1(tornado.web.RequestHandler):
    def get(self):
        pass


if __name__ == "__main__":
    application = tornado.web.Application(handlers=[
        (r"/", MainHandler),
        (r"/main1/", MainHandler1),
        #Meta API from the Application URIs
        (r"/api/v1/", api_v1),
    ])


    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

如何从class api_v1(tornado.web.RequestHandler)访问handlers变量。有可能吗?

我想在用户访问http://.../api/v1/时显示URLS模式

提前致谢。

最佳答案

传递到 Application 构造函数的处理程序表在事后不可用。相反,请在创建应用程序之前保存一份副本并将其提供给您的处理程序:

handlers = [...]
# Unrecognized keyword arguments end up in Application.settings; recognized ones
# get eaten.  Pass the handler table in twice, once for the Application itself
# and once for settings.
app = Application(handlers, handler_table=handlers)

并在处理程序中使用self.settings['handler_table']

关于python - 从 Tornado Python 中的 RequestHandler 访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417863/

相关文章:

c# - 为Elasticsearch日期字段提供空值

jquery - 事件监听器不适用于动态添加的元素

python - 创建特定的Python字典

python - Jedi-vim 自动只插入匹配的选项(需要禁用它)

python - 使用坐标列表进行索引的更多 Pythonic 方法

java - 在 Java 中克隆对象 [3 个问题]

python - 用排列在网格中的线图覆盖 matplotlib imshow

java 语法如 Object(){}

go - FileServer 处理程序是否仅服务于您指定目录中的内容?

c++ - 来自 boost::asio::async_write 的 WriteHandler 在连接断开时无法正常工作(防火墙/手动断开网络连接)