简体   繁体   English

运行简单的Python Tornado应用程序时初始化错误

[英]Initialize error when running a simple Python Tornado app

I am trying to learn how to use the python web framework Tornado. 我正在尝试学习如何使用python网络框架Tornado。 I am already familiar with flask but so far I have difficulty even getting a simple app to start. 我已经熟悉了flask,但是到目前为止,即使启动一个简单的应用程序,我也很难。 My directory structure is as follows: 我的目录结构如下:

  • App 应用
    • static 静态的
    • templates 模板
      • Testing.html -app.py Testing.html -app.py

My code in app.py is simply: 我在app.py中的代码很简单:

define("port", default=5000, help="run on the given port", type=int)

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler),
        ]
        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )
        super(Application, self).__init__(handlers, **settings)

class MainHandler(tornado.web.RedirectHandler):
    def get(self):
        self.render("Testing.html")

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

However when I run app.py I get the error: 但是,当我运行app.py时,出现错误:

ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/http1connection.py", line 238, in _read_message
delegate.finish()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/httpserver.py", line 289, in finish
self.delegate.finish()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 2047, in finish
self.execute()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 2067, in execute
**self.handler_kwargs)
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 187, in __init__
self.initialize(**kwargs)

TypeError: initialize() takes at least 2 arguments (1 given)

Why is this? 为什么是这样? I should mention it does not give me the error until I try to connect. 我应该提一下,在尝试连接之前,它不会给我错误。

Your MainHandler should inherit from RequestHandler, not RedirectHandler. 您的MainHandler应该继承自RequestHandler,而不是RedirectHandler。

(Details: the RedirectHandler requires two arguments, "self" and "target_path". You'd specify the target path in your handlers list in Application.__init__. Since you don't have that second argument in the handlers list and you're incorrectly inheriting from RedirectHandler, Tornado gets an exception.) (详细信息:RedirectHandler需要两个参数“ self”和“ target_path”。您将在Application .__ init__的处理程序列表中指定目标路径。由于您在处理程序列表中没有第二个参数,因此错误地继承自RedirectHandler,Tornado会获得异常。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM