There are many errors in the tornado helloworld program. Programming is a careful work.

第一种情况:
由tornado创建的HelloWorld程序
1  # 导入模块
2  from abc import ABC
3  import tornado.ioloop
4  import tornado.web
5  # 创建视图类
6  class HelloWordHandler(tornado.web.RequestHandler, ABC):
7       def get(self):
8          self.write('hello world')
9       # 创建路由
10      app00 = tornado.web.Application([
11             (r'/hello/', HelloWordHandler)]
12     )
13 if __name__ == '__main__':
14     # 设置端口
15     app00.listen(8888)
16     # 启动项目
17     tornado.ioloop.IOLoop.current().start()

After the program runs, enter: 127.0.0.1:8888/hello/ in the browser, and the screen displays the string: hello world. On the surface, this program is okay, but line 11 prompts that HelloWordHandler is undefined. The solution: move app00 on line 10 to the beginning of the line.

The second case:

1 # Import module
2 from abc import ABC
3 import tornado.ioloop
4 import tornado.web
5 # Create view class
6 class HelloWordHandler(tornado.web.RequestHandler, ABC):
7 def get(self):
8 self.write(' hello world')
9 if __name__ =='__main__':
10 # Create route
11 app00 = tornado.web.Application([
12 (r'/hello/', HelloWordHandler)]
13)
14 # Set port
15 app00.listen( 8888)
16 # Start project
17 tornado.ioloop.IOLoop.current().start()

After the above code runs, there is no error, but the following error occurs when accessing: WARNING: tornado.access: 404 GET /favicon.ico (127.0.0.1) 1.00ms, the reason is that the route is not found, the definition location is wrong, change it back The first way of writing, take it out of main, it's OK.

Guess you like

Origin blog.csdn.net/tswang6503/article/details/112767858