Python WSGI規定了Web服務器和Python Web應用程式或Web框架之間的標準介面,主要是為了促進Web應用程式在各種Web服務器上的可移植性,
上述這句話翻譯自Python官方的PEP333標準:PEP 333 -- Python Web Server Gateway Interface v1.0
WSGI介面概述
WSGI的含義:Web Server Gateway Interface(Web服務器網管介面),
WSGI介面包含兩方面:server/gateway端 及 application/framework端,后面直接使用server和application來說明,不再使用gateway和framework,server端直接呼叫application端提供的可呼叫物件,另外在server和application之間還可以有一種稱作middleware的中間件,中間件對于server來說就是一個application,但是對于application來說中間件卻是一個server,
上述可呼叫物件是指:函式、方法、類或者帶有__call__方法的實體,
以下分別介紹application端,Server端和middleware三個部分
Application端
函式、方法、類及帶有callable方法的實體等可呼叫物件都可以作為application物件,application物件接受兩個引數并且可以被多次呼叫,
引數
- environ:environ引數是一個字典物件,該物件必須是內置的Python字典,應用程式可以任意修改該字典,字典還必須包含某些WSGI必需的變數,
- start_response:由server提供的回呼函式,其作用是由application將狀態碼和回應頭回傳給server,這個函式有兩個必需的位置引數和一個可選引數,三個引數分別為status,response_headers和exc_info
start_response的三個引數的意義如下:
- status:HTTP 回應碼及訊息,例如status = '200 OK'
- response_headers:提供給客戶端的回應頭,需要封裝成list of tuple pairs 的形式
response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))]
- exc_info:Python sys.exc_info()元組
回傳值
application物件必須回傳一個回應體,回應體的形式是list of str,也就是說回傳值是由一個或多個字串組成的串列,
以下是一個函式作為application物件的例子
def simple_app(environ, start_response):
"""最簡單的application物件"""
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return ['Hello world!\n']
以下是一個類作為application物件的例子
class AppClass:
"""
AppClass()會回傳一個AppClass類物件作為application,然后在迭代的時候就會呼叫__iter__方法,然后就可以產生相同的輸出,
如果我們也可以實作__call__方法直接將實體當做application
"""
def __init__(self, environ, start_response):
self.environ = environ
self.start = start_response
def __iter__(self):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
self.start(status, response_headers)
yield "Hello world!\n"
Server端
WSGI server必須要呼叫application,而且要使用位置引數的形式呼叫application,同時,從application的協議要求可知:
-
WSGI server必須向application提供環境引數,因此,自身也必須能夠獲取環境引數,
-
WSGI server接收application的回傳值作為回應體,
最簡單的WSGI server為Python自帶的wsgiref.simple_server,
代碼
from wsgiref.simple_server import make_server
server = make_server('localhost', 8080, application)
server.serve_forever()
Middleware
中間件位于WSGI server和WSGI application之間,關于中間件的部分代碼參考:
- An Introduction to the Python Web Server Gateway Interface (WSGI)
代碼如下
from wsgiref.simple_server import make_server
def application(environ, start_response):
response_body = 'hello world!'
status = '200 OK'
response_headers = [
('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))
]
start_response(status, response_headers)
return [response_body]
# 中間件
class Upperware:
def __init__(self, app):
self.wrapped_app = app
def __call__(self, environ, start_response):
for data in self.wrapped_app(environ, start_response):
yield data.upper()
wrapped_app = Upperware(application)
httpd = make_server('localhost', 8051, wrapped_app)
httpd.serve_forever()
print 'end'
記得幫我點贊哦!
精心整理了計算機各個方向的從入門、進階、實戰的視頻課程和電子書,按照目錄合理分類,總能找到你需要的學習資料,還在等什么?快去關注下載吧!!!

念念不忘,必有回響,小伙伴們幫我點個贊吧,非常感謝,
我是職場亮哥,YY高級軟體工程師、四年作業經驗,拒絕咸魚爭當龍頭的斜杠程式員,
聽我說,進步多,程式人生一把梭
如果有幸能幫到你,請幫我點個【贊】,給個關注,如果能順帶評論給個鼓勵,將不勝感激,
職場亮哥文章串列:更多文章

本人所有文章、回答都與著作權保護平臺有合作,著作權歸職場亮哥所有,未經授權,轉載必究!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161250.html
標籤:Python
