在我的 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/