国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

深入解析Python中的WSGI接口

2024-04-25 20:34:01
字體:
來源:轉載
供稿:網友

概述

WSGI接口包含兩方面:server/gateway 及 application/framework。

server調用由application提供的可調用對象。

另外在server和application之間還可能有一種稱作middleware的中間件。

可調用對象是指:函數、方法、類或者帶有callable方法的實例。

關于application

函數、方法、類及帶有callable方法的實例等可調用對象都可以作為the application object。

WSGI協議要求:

the application object接受兩個參數且可以被多次調用

這兩個參數分別為:

1.CGI式的字典;

2.回調函數:application用來向server傳遞http狀態碼/消息/http頭

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

# 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

示例如下:

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。

middleware的概念非常接近decorator。

以一個路由的例子示例:

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:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 思茅市| 浦江县| 宁化县| 老河口市| 民县| 岳阳县| 嵊州市| 延寿县| 同江市| 托克托县| 大姚县| 江北区| 桂东县| 海盐县| 西藏| 白朗县| 亳州市| 星座| 西乡县| 梧州市| 昌黎县| 平乐县| 板桥市| 余姚市| 黄梅县| 伊吾县| 兴化市| 井陉县| 泗阳县| 文安县| 莱芜市| 石台县| 隆林| 观塘区| 嘉峪关市| 德保县| 凤翔县| 衢州市| 彭州市| 安多县| 合水县|