在龙卷风(python)中,我没有创建tornado.web.Application()的实例,而是尝试在使用init调用该类时对其进行更改。
import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
import os.path
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html", page_title="My Bookstore | HOME", header_text="Welcome to My Bookstore!")
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
)
tornado.web.Application(self, handlers, **settings)
if __name__ == "__main__":
tornado.options.parse_command_line()
#Note: not creating an instance of application here ('app'), just creating a list of handlers and a dict of settings and passing it to the superclass.
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
但是我遇到这个错误,
Traceback (most recent call last):
File "main.py", line 44, in <module>
http_server = tornado.httpserver.HTTPServer(Application())
File "main.py", line 27, in __init__
tornado.web.Application(self, handlers, **settings)
File "C:\Python\Python37\lib\site-packages\tornado\web.py", line 2065, in __init__
handlers = list(handlers or [])
TypeError: 'Application' object is not iterable
有什么错误,我该如何解决?
super
初始化子类并想使用父类的子类时应调用__init__
这个
tornado.web.Application(self, handlers, **settings)
应该
super().__init__(handlers, **settings)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句