Python WSGI

Paste_Image.png

WSGI[1] is not a server, a python module, a framework, an API or any kind of software. It is just an interface specification by which server and application communicate. Both server and application interface sides are specified in the PEP 3333. If an application (or framework or toolkit) is written to the WSGI spec then it will run on any server written to that spec.

這是wsgi.tutorial的解釋。WSGI只是一種協(xié)議接口,規(guī)定好server(可以考慮為nginx,apache) 和 application的交流方式。

Web App

web app其實(shí)就是各種框架,flask,Django,bottle,他們至少要提供這么一個callable對象(a function, a method, a class or an instance with an object.call() method),且此對象接收兩個必須參數(shù)

  • environ
    經(jīng)歷過古代寫cgi的同學(xué)應(yīng)該知道這個變量,其實(shí)就是一個字典,里面登記著用戶客戶端request的基本信息,隨便給幾個大家感受下,然而wsgi比基礎(chǔ)的cgi再多點(diǎn)key-value值
    environ points to a dictionary containing CGI like environment ,variables which is populated by the server for each,received request from the client
# Required CGI variables 
env['REQUEST_METHOD'] = self.request_method # GET 
env['PATH_INFO'] = self.path # /hello 
env['SERVER_NAME'] = self.server_name # localhost 
env['SERVER_PORT'] = str(self.server_port) # 8888
  • start_response
    is a callback function supplied by the server ,which takes the HTTP status and headers as arguments
    這也是一個callable函數(shù),可以由WSGI container(Middleware)提供,接收HTTP status, 和 一些頭部的信息,這個函數(shù)的作用最后就是設(shè)定http回包的頭部信息

def application(environ, start_response):
  start_response('200 OK', [('Content-Type', 'text/plain')]) 
  return ['Hello world!']


Middleware

類似的有這些WSGI container: uWSGI, flup, Gunicorn
The thing to remember here, is that your entire web application can be called with that one simple command up above. As such, other functions or classes can wrap around your web application object and do other things before and/or after your web application gets called. That’s really all WSGI middleware is.

這個Middleware的功能一般都放在了WSGI container層做了,上面這句英文解釋已經(jīng)說的很清楚了,這一層會調(diào)用web app提供好的application,并在調(diào)用前后做一些額外的操作,例如下面的操作。那么Python正好提供了一種技術(shù)裝飾器 (Decorator)
There is currently WSGI middleware that will:

  • Handle web application errors
  • Provide session support
  • Profile your web application
  • Deal with Login authentication
  • and Gzip the output

參考一下知乎某個用戶的回答:

作者:不求東西 鏈接
『Middleware』本質(zhì)上是一個『裝飾器 Decorator』,和Application類似它也是一個『Callable對象』,如果它有『call』方法,其簽名應(yīng)該是這樣的:def call(app: Application): transformedApp: Application

這個Middleware返回的類型是一個被『裝飾』過的Application(transformedApp變量),這個transformedApp所依賴的『environ』和『start_response』可以被當(dāng)前Middleware所在上層的Server/Middleware所注入。再看 這個Middleware的參數(shù),它也是一個Application(app變量),所以Middleware本身的一些附加的邏輯和數(shù)據(jù)也可以通過app的參數(shù)注入到下層的Application里。Middleware是可以嵌套使用的,比如有『mw1』和『mw2』兩個Middleware和『app』一個Application,就可以通過mw1(mw2(app))

返回一個新的Application,如果mw1和mw2是相互獨(dú)立的,嵌套順序理論上也可以互換。也可以使用Python的『Decorator』語法,直接裝飾app:@mw1@mw2def app(environ, start_response): ...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容