Tornado框架 源码基本分析

python2 的环境中 pip install tornado==1.2.1

1.2.1版本Tornado源码少,利于了解、

 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()
Tornado初始源码
新建目录,创建app.py,写入Tornado分析源码
!usr/bin/env python
# -*- coding:utf-8 -*-
#python2 中定义解码与环境
import tornado.ioloop
import tornado.web

#自定义视图类
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('login.html')
    def post(self, *args, **kwargs):
        #获取前段页面post传过来的值
        v = self.get_argument('username')
        print v
        #跳转到index
        self.redirect('/index.html')
#设置静态文件目录与模版目录
settings={
    'template_path':'temp',
    'static_path':'static',
}

apps = tornado.web.Application([
    (r'/login.html', Login),
    (r'/index.html', Index),
],  **settings#设置添加进路由系统中
    )

if __name__ == '__main__':
    apps.listen(8888)#监听8888端口
    tornado.ioloop.IOLoop.instance().start()#运行程序
Tornado分析源码
根目录下创建temp目录,写入html文件模版
<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>
登录html实例
原文地址:https://www.cnblogs.com/cou1d/p/12005299.html