https://blog.csdn.net/a312024054/article/details/52207367
tornado原理:
tornado的使用
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") class loginHandler(tornado.web.RequestHandler): def get(self): self.render("login.html") #post方法接受数据 def post(self, *args, **kwargs): v = self.get_argument('username') print(v) self.redirect('home.html') #配置静态目录 settings = { 'template_path': 'templates', 'static_path': 'static', 'static_url_prefix': '/ppp/', #使用前缀 } application = tornado.web.Application([ (r"/login", loginHandler), (r"/home.html", MainHandler), ], **settings) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
源码分析流程:
a:python app.py
1)加载路由关系: def add_handles
2)加载配置文件:
3)启动socket:
b:用户发请求
1)匹配路由:
2)执行指定类的方法: