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)

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