WSGI規範筆記

概述

WSGI接口包含兩方面:server/gateway 及 application/framework。
server調用由application提供的可調用對象
另外在server和application之間還可能有一種稱做middleware的中間件。
可調用對象是指:函數、方法、類或者帶有callable方法的實例。html


關於application

函數、方法、類及帶有callable方法的實例等可調用對象均可以做爲the application object。
WSGI協議要求:
the application object接受兩個參數能夠被屢次調用app

這兩個參數分別爲:
1.CGI式的字典;
2.回調函數:application用來向server傳遞http狀態碼/消息/http頭函數

另外協議要求可調用對象必須將響應體封裝成一個可迭代的strings返回。url

# the application object. 能夠使用其餘名字,
# 可是在使用mod_wsgi 時必須爲  "application"
def application( environ, start_response):
#  函數接受兩個參數:
# environ :包含有CGI 式環境變量的字典,由server負責提供內容
# start_response:由server提供的回調函數,其做用是將狀態碼和響應頭返回給server

# 構造響應體,以可迭代字符串形式封裝
    response_body = 'The request method was %s' % environ['REQUEST_METHOD']

# HTTP 響應碼及消息
   status = '200 OK'

# 提供給客戶端的響應頭.
# 封裝成list of tuple pairs 的形式:
# 格式要求:[(Header name, Header value)].
   response_headers = [('Content-Type', 'text/plain'),
                       ('Content-Length', str(len(response_body)))]

# 將響應碼/消息及響應頭經過傳入的start_reponse回調函數返回給server
   start_response(status, response_headers)

#  響應體做爲返回值返回
# 注意這裏被封裝到了list中.
   return [response_body]

關於server

從概述中能夠知道,WSGI server必需要調用application,同時,從application的協議要求可知:
1. WSGI server必須向application提供環境參數,所以,自身也必須可以獲取環境參數。
2. WSGI server接收application的返回值做爲響應體。
最簡單的WSGI server爲Python自帶的wsgiref.simple_server
示例以下:code

from wsgiref.simple_server import make_server
srv = make_server('localhost', 8080, hello_world)
srv.serve_forever()

關於middleware

middleware的概念沒有appllication和server那麼容易理解。
假設一個符合application標準的可調用對象,它接受可調用對象做爲參數,返回一個可調用對象的對象
那麼對於server來講,它是一個符合標準的可調用對象,所以是application。
而對於application來講,它能夠調用application,所以是server。
這樣的可調用對象稱爲middleware。server

middleware的概念很是接近decorator。htm

以一個路由的例子示例:中間件

import re

# 這是一個標準的application object
def index(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['index page']

# 這是一個標準的application object
def hello(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['hello page']

# 這是一個標準的application object
def not_found(environ, start_response):
    start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
    return ['Not Found Page']

# map urls to functions
urls = [
    (r'^$', index),
    (r'hello/?$', hello)
]
# 這是一個middleware
# 根據不一樣的route返回不一樣的application object
def application(environ, start_response):
    path = environ.get('PATH_INFO', '').lstrip('/')
    for regex, callback in urls:
        match = re.search(regex, path)
        if match is not None:
            return callback(environ, start_response)
    return not_found(environ, start_response)
相關文章
相關標籤/搜索