tornado是基于socket的开源web框架,python中可应用tornado搭建web应用,

  注意:1、使用前安装:pip3 install tronado,2、在文件中导入:tornado.ioploop、tornado.web,


  tornado搭建web应用的基本流程结构如下:

  start文件启动socket  —>  路由至具体事件句柄(继承类)  —>  运行相应 post \ get 方法  —>  获取提交的数据  —>  反馈相应信息


   注意:1、get 和 post 方式的主要区别:get在url中可以传递数据(格式:http://192.168.1.101:8888/index?user=hu&name=hi)而post不能

  (一)post方法实现:

  启动文件:hello_tornado.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web

UserList = []

# 具体事件句柄,
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        # self.write("hello,world!")
        # 1、导入html模板;2、添加静态样式;3、模板语言渲染
        self.render('index.html',userList = UserList,)

    def post(self, *args, **kwargs):
        user = self.get_argument('user')    # 获取提交数据信息
        UserList.append(user)
        self.render('index.html',userList = UserList,)


# 路径解析
settings = {
    "template_path":"templates",

    "static_path":"statics",
    "static_url_prefix":"/sss/",

}

# 路由至具体的事件句柄
application = tornado.web.Application([
    (r"/index",MainHandler),
],**settings)


# 开启服务器,监听
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
View Code

  模板文件:index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <link rel="stylesheet" href="sss/index_css.css">
    <script rel="stylesheet" src="sss/jquery-1.12.4.min.js"></script>
    <script rel="stylesheet" src="sss/index_js.js"></script>
</head>
<body>

    <h1>提交内容:</h1>
    <form method="post" action="/index">
        <input type="text" name="user">
        <input type="submit" value="提交"  />
    </form>

    <h1>展示内容:</h1>
    <ul style="list-style:none;">
        {% for item in userList %}
        <li onclick="col_B(this)">{{item}}</li>
        {% end %}
    </ul>

</body>
</html>
View Code

  (二)get方法实现:

  启动文件:hello_tornado_get.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web

UserList = []

# 具体事件句柄,
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        user = self.get_argument('user',None)    # 获取提交数据信息
        if user:
            UserList.append(user)
        self.render('index_get.html',userList = UserList,)


# 路径解析
settings = {
    "template_path":"templates",

    "static_path":"statics",
    "static_url_prefix":"/sss/",

}

# 路由至具体的事件句柄
application = tornado.web.Application([
    (r"/index",MainHandler),
],**settings)


# 开启服务器,监听
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
View Code

  模板文件:index_get.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <link rel="stylesheet" href="sss/index_css.css">
    <script rel="stylesheet" src="sss/jquery-1.12.4.min.js"></script>
    <script rel="stylesheet" src="sss/index_js.js"></script>
</head>
<body>

    <h1>提交内容:</h1>
    <form method="get" action="/index">
        <input type="text" name="user">
        <input type="submit" value="提交"  />
    </form>

    <h1>展示内容:</h1>
    <ul>
        {% for item in userList %}
        <li onclick="col_B(this)">{{item}}</li>
        {% end %}
    </ul>

</body>
</html>
View Code

  另外:在使用模板引擎的时候,CSS\JS 样式渲染时有{ },容易引起冲突,尽量避免,

  如上面,将ul的样式直接写在style中,效果无影响;但是写在CSS文件中导入,效果无法显示,


    2、项目文件的管理,常分为静态文件、模板文件、数据、启动文件等,

  


   3、信息在反馈前,需要完成:模板路径解析、静态样式渲染、模板语言渲染,

  (一)路径解析由settings设置,

  (二)模板语言使用特殊的格式:1、简单字符串:{{ }};2、字段或者函数:{% for、if、while、、、 %} {% end %},在渲染结束时必须添加end,

       模板语言还可以使用自定义函数或模块,首先import相应模块,然后使用settings进行配置

  start文件:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tornado.ioloop
import tornado.web
from modules import methods as mt
from modules import modules as md

UserList = []
data = 'LOWERCase'
# 具体事件句柄,
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        user = self.get_argument('user',None)    # 获取提交数据信息
        if user:
            UserList.append(user)
        self.render('index_get.html',data=data,userList = UserList,)  # 数据传递至html中模板引擎


# 路径解析
settings = {
    "template_path":"templates",

    "static_path":"statics",
    "static_url_prefix":"/sss/",

    "ui_methods":mt,  # 指定自定义函数
    "ui_modules":md,  # 指定自定义类
}

# 路由至具体的事件句柄
application = tornado.web.Application([
    (r"/index",MainHandler),
],**settings)


# 开启服务器,监听
if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
View Code

  模板文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <link rel="stylesheet" href="sss/index_css.css">
    <script rel="stylesheet" src="sss/jquery-1.12.4.min.js"></script>
    <script rel="stylesheet" src="sss/index_js.js"></script>
</head>
<body>
    <h1>提交内容:</h1>
    <form method="get" action="/index">
        <input type="text" name="user">
        <input type="submit" value="提交"  />
    </form>

    <h1>展示内容:</h1>
    <!--模板语言1、传值或表达式-->
    <h2>{{data}}</h2>
    <!--模板语言2、应用代码块-->
    <ul>
        {% for item in userList %}
            {% if item=='alex' %}
                <li style="color:blue" onclick="col_B(this)">{{item}}</li>
            {% else %}
                <li onclick="col_B(this)">{{item}}</li>
            {% end %}
        {% end %}
    </ul>
    <!--模板语言3、直接使用自定义函数 ,注意参数self   -->
    {{ func(data) }}
    <!--模板语言4、直接使用自定义类对象-->
    {% module custom() %}
</body>
</html>
View Code

  自定义类:

from tornado.web import UIModule
from tornado import escape

class custom(UIModule):
    def render(self, *args, **kwargs):
        return "1234"
View Code

  自定义函数:

def func(self,args):
    return args.lower()
View Code

   4、tornado有许多内置函数,其中使用最多的是:static_url,