我正在使用龙卷风,并且有一堆映射到不同网址的处理程序。例如 :
#mainfile.py
#imports
#...
application = tornado.web.Application([
(r"/", DefaultHandler),
(r"/somepath", SomepathHandler),
], debug=True)
if __name__ == "__main__":
tornado.options.parse_command_line()
port = int(os.environ.get("PORT", 8001))
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
现在,在 DefaultHandler 和 SomepathHandler 中,我不喜欢错误消息的显示方式,所以我决定像这样覆盖 write_error 方法:
#DefaultHandler.py
class DefaultHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.write("a nicer message")
def initialize(self):
#stuff
def get(self):
#more stuff, etc.
进而
#Somepathhandler.py
class SomepathHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.write("a nicer message")
这肯定会继续与其他处理程序。我阅读了 Tornado 文档,似乎我应该是 subclassing class tornado.web.RequestHandler(application, request, **kwargs)
,但没有成功地做到这一点。我不太确定课程应该去哪里,我怎样才能让它在我的应用程序中注册?
谢谢!