码迷,mamicode.com
首页 > 其他好文 > 详细

Tornado 基础安装及启动

时间:2017-07-31 15:48:10      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:direct   .com   request   art   16px   ref   1.2   font   2017年   

Tornado 基础安装及启动

Tornado的安装

    Python 2.7.x 安装学习

    手动安装: 下载 tornado-1.2.1.tar.gz

Bash
tar xvzf tornado-1.2.1.tar.gzcd tornado-1.2.1python setup.py buildsudo python setup.py install


#本段代码来自 海瑞博客http://www.hairuinet.com/Tornado/201703102/index.html
 

    Python 3.x安装学习

Bash
pip3 install tornado


#本段代码来自 海瑞博客http://www.hairuinet.com/Tornado/201703102/index.html
 

默认安装为最新版本,如果指定版本可以 tornado==1.2.1 加版本号指定

 

Tornado的格式

Hello world

Python
#!/usr/bin/env python# -*- coding=utf-8 -*-# blog:www.hairuinet.com# Version: 1.0__author__ = "HaiRui"import tornado.ioloopimport tornado.webclass MainHandler(tornado.web.RequestHandler):    def get(self):        self.write("Hello, world")application = tornado.web.Application([    (r"/", MainHandler),])if __name__ == "__main__":    application.listen(8888)    tornado.ioloop.IOLoop.instance().start()


#本段代码来自 海瑞博客http://www.hairuinet.com/Tornado/201703102/index.html
 

URL的配置

    application = tornado.web.Application([
        (r"/login.html", LoginHandler),
        (r"/index.html", MainHandler),
        (r"/", LoginHandler),
    ])

    默认值匹配到信息后直接找到后面类执行对应的方法。

    application.add_handlers(‘www.hairuineit.com‘,[
        (r"/login.html", LoginHandler),
        (r"/index.html", MainHandler),
    ])

    可以有前缀的话,可以处理必须以 www.hairuineit.com开头的url 再匹配里的内容,用于多域名的情况

 

模板配置

    settings = {
        ‘template_path‘: ‘templates‘,#模板地址
        ‘static_path‘: ‘static‘,#文件路径
        ‘static_url_prefix‘: ‘static‘,#静态文件路径名称
    }

    写完必须传入url内

    application = tornado.web.Application([
        (r"/login.html", LoginHandler),
        (r"/index.html", MainHandler),
        (r"/", LoginHandler),
    ],**settings)

 

编写逻辑

Python
class LoginHandler(tornado.web.RequestHandler):
    def get(self):
        name = ‘Hairui‘
        self.render("index.html",**{‘name‘:name})#渲染
        #相当于django里面的return返回的render
        
    def post(self, *args, **kwargs):
        v = self.get_argument(‘username‘)
        print(v)
        self.redirect(‘/index.html‘)


#本段代码来自 海瑞博客http://www.hairuinet.com/Tornado/201703102/index.html
 

前台

Markup
<h3>{{name}}</h3>


#本段代码来自 海瑞博客http://www.hairuinet.com/Tornado/201703102/index.html
 

 

Tornado 基础安装及启动

标签:direct   .com   request   art   16px   ref   1.2   font   2017年   

原文地址:http://www.cnblogs.com/xuaijun/p/7263203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!