【问题标题】:python + tornado : 404 on imported request handlerpython + tornado:导入请求处理程序上的 404
【发布时间】:2018-12-13 12:17:02
【问题描述】:

我正在尝试使用 python 构建一个龙卷风应用程序。我已经能够构建一个基本的路由系统:

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

class CountHandler(tornado.web.RequestHandler):
   def get(self):
      self.write("Count endpoint.")

if __name__ == "__main__":
   application = tornado.web.Application([
      (r"/", MainHandler),
      (r"/count", CountHandler),
  ])
  application.listen(8888)
  tornado.ioloop.IOLoop.instance().start()

我想要实现的是导入那些处理程序类(从特定的handlers 文件夹),而不是在这个文件中定义它们(它们可能会变得更大)。为此,我已将我的 CountHandler 类提取到它自己的单独文件中,并且我正在将其导入:

from handlers import CountHandler

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

if __name__ == "__main__":
   application = tornado.web.Application([
      (r"/", MainHandler),
      (r"/count", CountHandler),
  ])
  application.listen(8888)
  tornado.ioloop.IOLoop.instance().start()

CountHandler 类没有改​​变一点,但现在我的/count 端点上得到了 404。我做错了吗?

【问题讨论】:

  • 它应该可以工作。问题一定出在其他地方。您的handlers 文件夹是否有一个名为__init__.py 的文件?

标签: python tornado


【解决方案1】:

修好了!

在我的导入中,我导入了整个 CountHandler 库。因此,如果我想在我的代码中使用CountHandler 类,它应该是CountHandler.CountHandlerImportedLib.Myclass

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多