WSGI協議

WSGI協議

首先弄清下面幾個概念: WSGI:全稱是Web Server Gateway InterfaceWSGI不是服務器,python模塊,框架,API或者任何軟件,只是一種規範,描述web server如何與web application通信的規範。serverapplication的規範在PEP 3333中有具體描述。要實現WSGI協議,必須同時實現web server和web application,當前運行在WSGI協議之上的web框架有Bottle, Flask, Djangouwsgi:WSGI一樣是一種通信協議,是uWSGI服務器的獨佔協議,用於定義傳輸信息的類型(type of information),每一個uwsgi packet4byte爲傳輸信息類型的描述,與WSGI協議是兩種東西,據說該協議是fcgi協議的10倍快。 uWSGI:是一個web服務器,實現了WSGI協議、uwsgi協議、http協議等。

WSGI協議主要包括serverapplication兩部分:

  • WSGI server負責從客戶端接收請求,將request轉發給application,將application返回的response返回給客戶端;
  • WSGI application接收由server轉發的request,處理請求,並將處理結果返回給serverapplication中可以包括多個棧式的中間件(middlewares),這些中間件需要同時實現server與application,因此可以在WSGI服務器與WSGI應用之間起調節作用:對服務器來說,中間件扮演應用程序,對應用程序來說,中間件扮演服務器。

WSGI協議其實是定義了一種serverapplication解耦的規範,即可以有多個實現WSGI server的服務器,也可以有多個實現WSGI application的框架,那麼就可以選擇任意的serverapplication組合實現自己的web應用。例如uWSGIGunicorn都是實現了WSGI server協議的服務器,DjangoFlask是實現了WSGI application協議的web框架,可以根據項目實際情況搭配使用。

DjangoFlask框架都有自己實現的簡單的WSGI server,一般用於服務器調試,生產環境下建議用其他WSGI server

以下內容轉載自V2EX:https://www.v2ex.com/t/375771

- WSGI 
- uWSGI 
- uwsgi 
- Nginx 

WSGI 是一種協議,不是任何包不是任何服務器,就和 TCP 協議一樣。它定義了 Web 服務器和 Web 應用程序之前如何通信的規範。 

至於爲什麼和 Python 扯在一起?因爲這個協議是由 Python 在 2003 年提出的。(參考:PEP-333 和 PEP-3333 ) 

> WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. 

uWSGI 是一個全功能的 HTTP 服務器,他要做的就是把 HTTP 協議轉化成語言支持的網絡協議。比如把 HTTP 協議轉化成 WSGI 協議,讓 Python 可以直接使用。 

> The uWSGI project aims at developing a full stack for building hosting services. 

> Application servers (for various programming languages and protocols), proxies, process managers and monitors are all implemented using a common api and a common configuration style. 

uwsgi 是一種 uWSGI 的內部協議,使用二進制方式和其他應用程序進行通信。 

> The uwsgi (lowercase!) protocol is the native protocol used by the uWSGI server. 

> It is a binary protocol that can carry any type of data. The first 4 bytes of a uwsgi packet describe the type of the data contained by the packet. 

Nginx 是一個 Web 服務器其中的 HTTP 服務器功能和 uWSGI 功能很類似,但是 Nginx 還可以用作更多用途,比如最常用的反向代理功能。 


**接下是爲什麼不能用 Django 的 Web 服務器直接部署** 

Django 是一個 Web 框架,框架的作用在於處理 request 和 reponse,其他的不是框架所關心的內容。所以怎麼部署 Django 不是 Django 所需要關心的。 

Django 所提供的是一個開發服務器,這個開發服務器,沒有經過安全測試,而且使用的是 Python 自帶的 simple HTTPServer 創建的,在安全性和效率上都是不行的。 

> DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. 

在 Django 源碼中可以很清楚的看出來,runserver 起來的 HTTPServer 就是 Python 自帶的 simple_server。 

> 以下是最新版本 Django 有關 runserver command 的代碼節選 

- [django.core.management.commands.runserver.Command:run]( https://github.com/django/django/blob/master/django/core/management/commands/runserver.py#L100-L107
- [django.core.management.commands.runserver.Command:inner_run]( https://github.com/django/django/blob/master/django/core/management/commands/runserver.py#L141-L142

其中 inner_run 函數中的 run 方法和 run 方法中 server_cls 參數分別取自 [django.core.servers.basehttp:run]( https://github.com/django/django/blob/master/django/core/servers/basehttp.py#L164-L180) 和 [django.core.servers.basehttp:WSGIServer]( https://github.com/django/django/blob/master/django/core/servers/basehttp.py#L57-L73

而 WSGIServer 又的父類就是 wsgiref.simple_server。既然是 simple 了很多東西都是不太可以的。 

**既然 uWSGI 可以完成 Nginx 功能,那爲什麼又要用 Nginx** 

因爲 Nginx 牛逼啊,能直接在 Ninx 層面就完成很多事情,比如靜態文件、反向代理、轉發等需求。 

 

那爲什麼用了Nginx又要用uWSGI或者gunicorn呢?

因爲標準 Python 只能是是單線程,無法併發,而 uwsgi / gunicorn 通過多進程池達成了更暴力的併發。 

## 參考 

- [WSGI 官方文檔]( https://wsgi.readthedocs.io/en/latest/index.html
- [uWSGI 官方文檔]( http://uwsgi-docs.readthedocs.io/en/latest/index.html
- [Django django-admin runserver]( https://docs.djangoproject.com/en/1.11/ref/django-admin/#runserver)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章