Tornado 框架的使用

Tornado

tornado是一個輕量級python的web框架,他是非阻塞式的,並且速度很是快.得利於其 非阻塞的方式和對 epoll 的運用,Tornado 每秒能夠處理數以千計的鏈接,這意味着對於實時 Web 服務來講,Tornado 是一個理想的 Web 框架。javascript

1.最根本的使用

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

import tornado.ioloop
import tornado.web  

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

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


if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

2.路由系統

tornado的路由系統其實就是 url 和 類 的對應關係,這裏不一樣於其餘框架,其餘不少框架均是 url 對應 函數,Tornado中每一個url對應的是一個類。css

application = tornado.web.Application([
    (r"/index", MainHandler),
    (r"/story/([0-9]+)", StoryHandler),
])

application.add_handlers('news.baidu.com$', [
    (r'/index',NewsHandler),
])

Tornado中原生支持二級域名的路由html

3.模版引擎

Tornado 的模板支持「控制語句」和「表達語句」,控制語句是使用 {% 和 %} 包起來的 前端

例如 {% if len(items) > 2 %}。java

表達語句是使用 {{ 和 }} 包起來的,python

例如 {{ items[0] }}。web

Tornado控制語句和對應的 Python 語句的格式基本徹底相同。咱們支持 if、for、while 和 try,這些語句邏輯結束的位置須要用 {% end %} 作標記。還經過 extends 和 block 語句實現了模板繼承。算法

在使用模板前須要在setting中設置模板路徑:"template_path" : "tpl"json

例如:緩存

邏輯語句
<ul>
    {% for item in list_info %}
        <li>{{item}}</li>
    {% end %}
</ul>

-----------------------------------------   
繼承母版
{% block RenderBody %}{% end %}

{% block RenderBody %}
<h1>Index</h1>

在這裏寫內容

{% end %}
-----------------------------------------

導入固定的小組件
{% include 'header.html' %}

在模板中默認提供了一些函數、字段、類以供模板使用:

escape: tornado.escape.xhtml_escape 的別名
xhtml_escape: tornado.escape.xhtml_escape 的別名
url_escape: tornado.escape.url_escape 的別名
json_encode: tornado.escape.json_encode 的別名
squeeze: tornado.escape.squeeze 的別名
linkify: tornado.escape.linkify 的別名
datetime: Python 的 datetime 模組
handler: 當前的 RequestHandler 對象
request: handler.request 的別名
current_user: handler.current_user 的別名
locale: handler.locale 的別名
_: handler.locale.translate 的別名
static_url: for handler.static_url 的別名
xsrf_form_html: handler.xsrf_form_html 的別名

固然了,還能夠自定義本身的功能

4.自定義UIMethod以UIModule

  • UIMethod:方便小巧,適合簡單的方法

    # uimethods.py
    
    def tab(self):
        return 'UIMethod'
    ------------------------------
    導入和註冊
    import uimethods as mt
    
    settings = {
        ......,
        'ui_methods': mt,
    }
    ------------------------------
    使用就很方便了
    {{ tab() }} #記住要加()
  • UIModule:模塊化,適合組合模塊

    from tornado.web import UIModule
    from tornado import escape
    
    class custom(UIModule):
    
        def render(self, *args, **kwargs):
            '''直接返回內容'''
    
        def css_files(self):
            '''返回一個引入式css路徑'''
    
        def embedded_css(self):
            '''以字符串的方式返回一個嵌入式的css'''
    
        def javascript_files(self):
            '''返回一個引入式JS路徑'''
    
        def embedded_javascript(self):
            '''以字符串的方式返回css語句'''
    
    ------------------------------
    導入和註冊
    import uimodules as md
    
    settings = {
        ......,
        'ui_modules': md,
    }
    ------------------------------
    使用
    {% module custom(123) %} # 能夠傳參

5.靜態文件

對於靜態文件,能夠配置靜態文件的目錄和前段使用時的前綴,而且Tornaodo還支持靜態文件緩存。

在設置中寫上:

settings = {
'template_path': 'template',
'''模版路徑'''
'static_path': 'static',
'''實際靜態文件路徑'''
'static_url_prefix': '/static/',
'''靜態文件的默認識別前綴,意思是靜態文件的路徑無論是什麼,這要這裏寫了/static/,
    前端無論真實路徑,直接用/static/,tornado會自動查找真實路徑'''
}
--------------------------------------------
前端使用
<link href="/static/example.css" rel="stylesheet" />

爲避免出現前綴修改後前端所有都要修改的狀況,還能夠用一個方法,使前端能夠自動生成路徑的前綴
<link href="{{ static_url("example.css") }}" rel="stylesheet" />

6.Cookie

Tornado中能夠對cookie進行操做,而且還能夠對cookie進行簽名以放置僞造。

self.set_cookie("mycookie", "myvalue")
'''設置cookie'''
self.set_secure_cookie("mycookie", "myvalue")
'''設置加密cookie'''

設置cookie的時候能夠加時間戳參數expires=...,意爲到這個時間cookie失效
    或者expires_day=3,則過時時間是3天,同時設置的話expires優先級更高

注意:設置加密cookie的時候,需在setting中設置加密簽名
    settings = {
        ......,
        'cookie_secret':'本身填',
    }
--------------------------------------------
self.get_cookie('mycookie')
'''獲取cookie'''
self.get_secure_cookie('mycookie')
'''獲取加密cookie'''

簽名Cookie的本質是:

寫cookie過程:

將值進行base64加密
對除值之外的內容進行簽名,哈希算法(沒法逆向解析)
拼接 簽名 + 加密值

讀cookie過程:

讀取 簽名 + 加密值
對簽名進行驗證
base64解密,獲取值內容

7.XSRF

Tornado中的跨站請求僞造和Django中的類似.

設置:

settings = {
    ...
    "xsrf_cookies": True,
}

使用:

<form action="/new_message" method="post">
  {{ xsrf_form_html() }}
  <input type="text"/>
</form>

Ajax使用就在本地獲取cookie,攜帶cookie發送請求
_xsrf = getCookie("_xsrf")

8.異步非阻塞

裝飾器 + Future 從而實現Tornado的異步非阻塞

class AsyncHandler(tornado.web.RequestHandler):

    @gen.coroutine
    def get(self):
        future = Future()
        future.add_done_callback(self.doing)
        yield future
        # 或
        # tornado.ioloop.IOLoop.current().add_future(future,self.doing)
        # yield future

    def doing(self,*args, **kwargs):
        self.write('async')
        self.finish()

當發送GET請求時,因爲方法被@gen.coroutine裝飾且yield 一個 Future對象,那麼Tornado會等待,等待用戶向future對象中放置數據或者發送信號,若是獲取到數據或信號以後,就開始執行doing方法。

異步非阻塞體如今當在Tornaod等待用戶向future對象中放置數據時,還能夠處理其餘請求。

注意:在等待用戶向future對象中放置數據或信號時,此鏈接是不斷開的。

其餘

tornado中的請求的一切均可以在self.request中找到.

待定

相關文章
相關標籤/搜索