WSGI協議
首先弄清下面幾個概念:
WSGI:全稱是Web Server Gateway Interface
,WSGI
不是服務器,python
模塊,框架,API
或者任何軟件,只是一種規范,描述web server
如何與web application
通信的規范。server
和application
的規范在PEP 3333中有具體描述。要實現WSGI協議,必須同時實現web server和web application,當前運行在WSGI
協議之上的web
框架有Bottle
, Flask
, Django
。
uwsgi:與WSGI
一樣是一種通信協議,是uWSGI
服務器的獨占協議,用於定義傳輸信息的類型(type of information
),每一個uwsgi packet
前4byte
為傳輸信息類型的描述,與WSGI協議是兩種東西,據說該協議是fcgi
協議的10倍快。
uWSGI:是一個web
服務器,實現了WSGI
協議、uwsgi
協議、http
協議等。
WSGI
協議主要包括server
和application
兩部分:
WSGI server
負責從客戶端接收請求,將request
轉發給application
,將application
返回的response
返回給客戶端;WSGI application
接收由server
轉發的request
,處理請求,並將處理結果返回給server
。application
中可以包括多個棧式的中間件(middlewares
),這些中間件需要同時實現server與application,因此可以在WSGI服務器與WSGI應用之間起調節作用:對服務器來說,中間件扮演應用程序,對應用程序來說,中間件扮演服務器。
WSGI
協議其實是定義了一種server
與application
解耦的規范,即可以有多個實現WSGI server
的服務器,也可以有多個實現WSGI application
的框架,那么就可以選擇任意的server
和application
組合實現自己的web
應用。例如uWSGI
和Gunicorn
都是實現了WSGI server
協議的服務器,Django
,Flask
是實現了WSGI application
協議的web
框架,可以根據項目實際情況搭配使用。
像Django
,Flask
框架都有自己實現的簡單的WSGI server
,一般用於服務器調試,生產環境下建議用其他WSGI server
。
WSGI協議的實現
以Django
為例,分析一下WSGI
協議的具體實現過程。
django WSGI application
WSGI application
應該實現為一個可調用對象,例如函數、方法、類(包含**call**
方法)。需要接收兩個參數:
- 一個字典,該字典可以包含了客戶端請求的信息以及其他信息,可以認為是請求上下文,一般叫做
environment
(編碼中多簡寫為environ
、env
) - 一個用於發送HTTP響應狀態(
HTTP status
)、響應頭(HTTP headers
)的回調函數
通過回調函數將響應狀態和響應頭返回給server
,同時返回響應正文(response body
),響應正文是可迭代的、並包含了多個字符串。下面是Django
中application
的具體實現部分:
class WSGIHandler(base.BaseHandler):
initLock = Lock()
request_class = WSGIRequest
def __call__(self, environ, start_response):
# 加載中間件
if self._request_middleware is None:
with self.initLock:
try:
# Check that middleware is still uninitialized.
if self._request_middleware is None:
self.load_middleware()
except:
# Unload whatever middleware we got
self._request_middleware = None
raise
set_script_prefix(get_script_name(environ))
# 請求處理之前發送信號
signals.request_started.send(sender=self.__class__, environ=environ)
try:
request = self.request_class(environ)
except UnicodeDecodeError:
logger.warning('Bad Request (UnicodeDecodeError)',
exc_info=sys.exc_info(),
extra={'status_code': 400,})
response = http.HttpResponseBadRequest()
else:
response = self.get_response(request)
response._handler_class = self.__class__
status = '%s %s' % (response.status_code, response.reason_phrase)
response_headers = [(str(k), str(v)) for k, v in response.items()]
for c in response.cookies.values():
response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
# server提供的回調方法,將響應的header和status返回給server
start_response(force_str(status), response_headers)
if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):
response = environ['wsgi.file_wrapper'](response.file_to_stream)
return response
可以看出application
的流程包括:
- 加載所有中間件,以及執行框架相關的操作,設置當前線程腳本前綴,發送請求開始信號;
- 處理請求,調用
get_response()
方法處理當前請求,該方法的的主要邏輯是通過urlconf
找到對應的view
和callback
,按順序執行各種middleware
和callback
。 - 調用由
server
傳入的start_response()
方法將響應header
與status
返回給server
。 - 返回響應正文
django WSGI Server
負責獲取http
請求,將請求傳遞給WSGI application
,由application
處理請求后返回response
。以Django
內建server
為例看一下具體實現。
通過runserver
運行django
項目,在啟動時都會調用下面的run
方法,創建一個WSGIServer
的實例,之后再調用其serve_forever()
方法啟動服務。
def run(addr, port, wsgi_handler, ipv6=False, threading=False):
server_address = (addr, port)
if threading:
httpd_cls = type(str('WSGIServer'), (socketserver.ThreadingMixIn, WSGIServer), {})
else:
httpd_cls = WSGIServer
# 這里的wsgi_handler就是WSGIApplication
httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
if threading:
httpd.daemon_threads = True
httpd.set_app(wsgi_handler)
httpd.serve_forever()
下面表示WSGI server
服務器處理流程中關鍵的類和方法。
xiong (2).png-93.7kB
-
WSGIServer
run()
方法會創建
WSGIServer
實例,主要作用是接收客戶端請求,將請求傳遞給
application
,然后將
application
返回的
response
返回給客戶端。
- 創建實例時會指定
HTTP
請求的handler
:WSGIRequestHandler
類 - 通過
set_app
和get_app
方法設置和獲取WSGIApplication
實例wsgi_handler
- 處理http請求時,調用
handler_request
方法,會創建WSGIRequestHandler
實例處理http請求。 WSGIServer
中get_request
方法通過socket
接受請求數據
- 創建實例時會指定
-
WSGIRequestHandler
- 由
WSGIServer
在調用handle_request
時創建實例,傳入request
、cient_address
、WSGIServer
三個參數,__init__
方法在實例化同時還會調用自身的handle
方法 handle
方法會創建ServerHandler
實例,然后調用其run
方法處理請求
- 由
-
ServerHandler
WSGIRequestHandler
在其handle
方法中調用run
方法,傳入self.server.get_app()
參數,獲取WSGIApplication
,然后調用實例(__call__
),獲取response
,其中會傳入start_response
回調,用來處理返回的header
和status
。- 通過
application
獲取response
以后,通過finish_response
返回response
-
WSGIHandler
WSGI
協議中的application
,接收兩個參數,environ
字典包含了客戶端請求的信息以及其他信息,可以認為是請求上下文,start_response
用於發送返回status
和header
的回調函數
雖然上面一個WSGI server
涉及到多個類實現以及相互引用,但其實原理還是調用WSGIHandler
,傳入請求參數以及回調方法start_response()
,並將響應返回給客戶端。
django simple_server
django
的simple_server.py
模塊實現了一個簡單的HTTP
服務器,並給出了一個簡單的demo
,可以直接運行,運行結果會將請求中涉及到的環境變量在瀏覽器中展示出來。
其中包括上述描述的整個http
請求的所有組件:
ServerHandler
, WSGIServer
, WSGIRequestHandler
,以及demo_app
表示的簡易版的WSGIApplication
。
可以看一下整個流程:
if __name__ == '__main__':
# 通過make_server方法創建WSGIServer實例
# 傳入建議application,demo_app
httpd = make_server('', 8000, demo_app)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
import webbrowser
webbrowser.open('http://localhost:8000/xyz?abc')
# 調用WSGIServer的handle_request方法處理http請求
httpd.handle_request() # serve one request, then exit
httpd.server_close()
def make_server(
host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler
):
"""Create a new WSGI server listening on `host` and `port` for `app`"""
server = server_class((host, port), handler_class)
server.set_app(app)
return server
# demo_app可調用對象,接受請求輸出結果
def demo_app(environ,start_response):
from io import StringIO
stdout = StringIO()
print("Hello world!", file=stdout)
print(file=stdout)
h = sorted(environ.items())
for k,v in h:
print(k,'=',repr(v), file=stdout)
start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')])
return [stdout.getvalue().encode("utf-8")]
demo_app()
表示一個簡單的WSGI application實現,通過make_server()
方法創建一個WSGIServer
實例,調用其handle_request()
方法,該方法會調用demo_app()
處理請求,並最終返回響應。
uWSGI
uWSGI
旨在為部署分布式集群的網絡應用開發一套完整的解決方案。主要面向web
及其標准服務。由於其可擴展性,能夠被無限制的擴展用來支持更多平台和語言。uWSGI
是一個web
服務器,實現了WSGI
協議,uwsgi
協議,http
協議等。
uWSGI
的主要特點是:
- 超快的性能
- 低內存占用
- 多
app
管理 - 詳盡的日志功能(可以用來分析
app
的性能和瓶頸) - 高度可定制(內存大小限制,服務一定次數后重啟等)
uWSGI
服務器自己實現了基於uwsgi
協議的server
部分,我們只需要在uwsgi
的配置文件中指定application
的地址,uWSGI
就能直接和應用框架中的WSGI application
通信。
參考資料:
做python Web開發你要理解:WSGI & uwsgi
WSGI & uwsgi
WSGI Tutorial
打造mvc框架之wsgi協議的優缺點及接口實現
Nginx和uWSGI通信機制
理解Python WSGI