深入解析Python中的WSGI接口
概述
WSGI接口包含兩方面:server/gateway 及 application/framework。
server調(diào)用由application提供的可調(diào)用對象。
另外在server和application之間還可能有一種稱作middleware的中間件。
可調(diào)用對象是指:函數(shù)、方法、類或者帶有callable方法的實例。
關于application
函數(shù)、方法、類及帶有callable方法的實例等可調(diào)用對象都可以作為the application object。
WSGI協(xié)議要求:
the application object接受兩個參數(shù)且可以被多次調(diào)用
這兩個參數(shù)分別為:
1.CGI式的字典;
2.回調(diào)函數(shù):application用來向server傳遞http狀態(tài)碼/消息/http頭
另外協(xié)議要求可調(diào)用對象必須將響應體封裝成一個可迭代的strings返回。
# the application object. 可以使用其他名字,
# 但是在使用mod_wsgi 時必須為 "application"
def application( environ, start_response):
# 函數(shù)接受兩個參數(shù):
# environ :包含有CGI 式環(huán)境變量的字典,由server負責提供內(nèi)容
# start_response:由server提供的回調(diào)函數(shù),其作用是將狀態(tài)碼和響應頭返回給server
# 構(gòu)造響應體,以可迭代字符串形式封裝
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回調(diào)函數(shù)返回給server
start_response(status, response_headers)
# 響應體作為返回值返回
# 注意這里被封裝到了list中.
return [response_body]
關于server
從概述中可以知道,WSGI server必須要調(diào)用application,同時,從application的協(xié)議要求可知:
1. WSGI server必須向application提供環(huán)境參數(shù),因此,自身也必須能夠獲取環(huán)境參數(shù)。
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標準的可調(diào)用對象,它接受可調(diào)用對象作為參數(shù),返回一個可調(diào)用對象的對象。
那么對于server來說,它是一個符合標準的可調(diào)用對象,因此是application。
而對于application來說,它可以調(diào)用application,因此是server。
這樣的可調(diào)用對象稱為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
# 根據(jù)不同的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:
相關文章
Pandas查詢數(shù)據(jù)df.query的使用
本文主要介紹了Pandas查詢數(shù)據(jù)df.query的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
解決Vscode中jupyter出現(xiàn)kernel dead問題
遇到VSCode中Jupyter Kernel Dead時,可通過Anaconda Prompt安裝ipykernel解決,首先使用jupyter kernelspec list命令查看內(nèi)核,若發(fā)現(xiàn)缺少ipykernel,激活相應虛擬環(huán)境,使用conda install ipykernel命令安裝,操作后,VSCode中Jupyter應能正常運行2024-09-09
Python 讀取.shp文件并生成圖幅編號的實現(xiàn)思路
這篇文章主要介紹了Python 讀取.shp文件并生成圖幅編號,代碼適用于需要處理和分析地理空間數(shù)據(jù)的場景,如城市規(guī)劃、環(huán)境監(jiān)測或自然資源管理,其中它可以幫助用戶讀取特定區(qū)域的Shapefile文件,確定其地理邊界,需要的朋友可以參考下2024-05-05
Python實現(xiàn)yaml與json文件批量互轉(zhuǎn)
這篇文章主要為大家詳細介紹了如何利用Python語言實現(xiàn)yaml與json文件的批量互轉(zhuǎn),文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下2022-07-07

