In python2 environment pip install tornado == 1.2.1
Tornado less version 1.2.1 source code, which will help to understand,
1 import tornado.ioloop 2 import tornado.web 3 4 class MainHandler(tornado.web.RequestHandler): 5 def get(self): 6 self.write("Hello, world") 7 8 if __name__ == "__main__": 9 application = tornado.web.Application([ 10 (r"/", MainHandler), 11 ]) 12 application.listen(8888) 13 tornado.ioloop.IOLoop.instance().start()
New directory, create app.py, write source code analysis Tornado
! usr / bin / env Python # - * - Coding: UTF-8 - * - # python2 defined in decoding and environment Import tornado.ioloop Import tornado.web # Custom View class class Index (tornado.web.RequestHandler): DEF GET (Self, * args, ** kwargs): self.write('hello index') class Login(tornado.web.RequestHandler): def get(self, *args, **kwargs): self.render ( ' the login.html ' ) DEF post (Self, args *, ** kwargs): # Get the preceding page post pass over the value V = self.get_argument ( ' username ' ) Print V # Jump to index Self .redirect ( ' /index.html ' ) # set static files catalog template directory settings = { ' template_path ' : ' TEMP ' , ' static_path ' : ' static ', } apps = tornado.web.Application([ (r'/login.html', Login), (r'/index.html', Index), ], ** Settings # Set added to the routing system ) if __name__ == '__main__': apps.listen ( 8888) # listen 8888 port tornado.ioloop.IOLoop.instance (). Start () # run the program
Create a temp directory in the root directory, write html template file
<head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>登录</h1> <form method="post" action="/login.html"> <p><input type="text" name="username"></p> <p><input type="text" name="password"></p> <p><input type="submit" value="提交"> </p> </form> </body> </html>