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

首頁(yè) > 編程 > Python > 正文

深入解析Python中的WSGI接口

2020-02-23 01:10:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

概述

WSGI接口包含兩方面:server/gateway 及 application/framework。
server調(diào)用由application提供的可調(diào)用對(duì)象。
另外在server和application之間還可能有一種稱(chēng)作middleware的中間件。
可調(diào)用對(duì)象是指:函數(shù)、方法、類(lèi)或者帶有callable方法的實(shí)例。
關(guān)于application

函數(shù)、方法、類(lèi)及帶有callable方法的實(shí)例等可調(diào)用對(duì)象都可以作為the application object。
WSGI協(xié)議要求:
the application object接受兩個(gè)參數(shù)且可以被多次調(diào)用

這兩個(gè)參數(shù)分別為:
1.CGI式的字典;
2.回調(diào)函數(shù):application用來(lái)向server傳遞http狀態(tài)碼/消息/http頭

另外協(xié)議要求可調(diào)用對(duì)象必須將響應(yīng)體封裝成一個(gè)可迭代的strings返回。

# the application object. 可以使用其他名字,# 但是在使用mod_wsgi 時(shí)必須為 "application"def application( environ, start_response):# 函數(shù)接受兩個(gè)參數(shù):# environ :包含有CGI 式環(huán)境變量的字典,由server負(fù)責(zé)提供內(nèi)容# start_response:由server提供的回調(diào)函數(shù),其作用是將狀態(tài)碼和響應(yīng)頭返回給server# 構(gòu)造響應(yīng)體,以可迭代字符串形式封裝  response_body = 'The request method was %s' % environ['REQUEST_METHOD']# HTTP 響應(yīng)碼及消息  status = '200 OK'# 提供給客戶端的響應(yīng)頭.# 封裝成list of tuple pairs 的形式:# 格式要求:[(Header name, Header value)].  response_headers = [('Content-Type', 'text/plain'),            ('Content-Length', str(len(response_body)))]# 將響應(yīng)碼/消息及響應(yīng)頭通過(guò)傳入的start_reponse回調(diào)函數(shù)返回給server  start_response(status, response_headers)# 響應(yīng)體作為返回值返回# 注意這里被封裝到了list中.  return [response_body]

關(guān)于server

從概述中可以知道,WSGI server必須要調(diào)用application,同時(shí),從application的協(xié)議要求可知:
1. WSGI server必須向application提供環(huán)境參數(shù),因此,自身也必須能夠獲取環(huán)境參數(shù)。
2. WSGI server接收application的返回值作為響應(yīng)體。
最簡(jiǎn)單的WSGI server為Python自帶的wsgiref.simple_server
示例如下:

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

關(guān)于middleware

middleware的概念沒(méi)有appllication和server那么容易理解。
假設(shè)一個(gè)符合application標(biāo)準(zhǔn)的可調(diào)用對(duì)象,它接受可調(diào)用對(duì)象作為參數(shù),返回一個(gè)可調(diào)用對(duì)象的對(duì)象。
那么對(duì)于server來(lái)說(shuō),它是一個(gè)符合標(biāo)準(zhǔn)的可調(diào)用對(duì)象,因此是application。
而對(duì)于application來(lái)說(shuō),它可以調(diào)用application,因此是server。
這樣的可調(diào)用對(duì)象稱(chēng)為middleware。

middleware的概念非常接近decorator。

以一個(gè)路由的例子示例:

import re# 這是一個(gè)標(biāo)準(zhǔn)的application objectdef index(environ, start_response):  start_response('200 OK', [('Content-Type', 'text/html')])  return ['index page']# 這是一個(gè)標(biāo)準(zhǔn)的application objectdef hello(environ, start_response):  start_response('200 OK', [('Content-Type', 'text/html')])  return ['hello page']# 這是一個(gè)標(biāo)準(zhǔn)的application objectdef not_found(environ, start_response):  start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])  return ['Not Found Page']# map urls to functionsurls = [  (r'^$', index),  (r'hello/?$', hello)]# 這是一個(gè)middleware# 根據(jù)不同的route返回不同的application objectdef 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:            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清镇市| 庄河市| 沿河| 师宗县| 庆阳市| 绥中县| 呼和浩特市| 濮阳市| 多伦县| 刚察县| 蚌埠市| 黄梅县| 嘉禾县| 凭祥市| 祁东县| 大名县| 罗城| 吴桥县| 利川市| 会泽县| 定襄县| 凤庆县| 香河县| 济宁市| 五家渠市| 兴海县| 诸暨市| 青神县| 宁强县| 河北省| 盘锦市| 东阳市| 综艺| 承德市| 乐安县| 湖南省| 玉环县| 德格县| 丰台区| 英吉沙县| 武乡县|