美文网首页
浅析WSGI

浅析WSGI

作者: 四月白绵羊 | 来源:发表于2019-10-15 02:58 被阅读0次

    今天看了一篇关于WSGI的英语文章,写的很清晰,总结一下以便日后翻阅。

    WSGI Servers

    Web Server Gateway Interface服务器实际上把WSGI接口包装成了服务器,可以用来运行Python web applications。

    Why is WSGI necessary?

    传统的网页服务器没有办法运行python应用。在20世纪末期,Grisha Trubetskoy开发了一个Apache模块,mode_python来运行任意的python代码。但是这样也只是实现了从无到有的实现,并不是一个标准的约定。于是Python社区发明了WSGI作为标准的接口。这个接口用于Web Server 和 Python web application之间的通信连接。


    image.png

    如图中所示,一个WSGI服务器会根据PEP 3333 standard,调用Python app。

    WSGI's Purpose

    那么为什么要使用WSGI,而不是让web app直接和web server进行通信呢?

    • WSGI 使你的Web App更加灵活:开发者或许会替换他们技术站中的元素。比如我现在不想使用Gunicorn了,换成了uWSGI了。那么我不需要去修改Web App的接口,因为Gunicorn 和 uWSGI都是基于WSGI接口实现的。
      就像在PEP(Python Enhancement Proposal) 3333 中提到的:
      由于WSGI接口的广泛使用,开发者选择Web框架的选择可以不在那么依赖于Web Server了。开发者只需要选择合适的Web Server和这个Server 支持的CGI接口,就可以进行自由地配对。另一方面让术业有专攻,Web Server开发者可以专注于自己的领域,Web App开发者亦是。

    • WSGI服务器提高了Web App 的适应性Scaling: 服务成千上万的请求是WSGI服务器的工作,而不是Web App的工作。Web App只专注于逻辑处理而已。如果分发这些请求,使之得到高效的处理是WSGI的工作之一。

    image.png

    上图是Web Browser,Web Server 和 WSGI Server之间的通信流程。

    WSGI就是一个用来运行Python代码的标准接口。而作为一个网页开发者,你所需要知道的是:

    • WSGI代表了Web Server Gateway Interface.
    • WSGI服务器是一个单独的进城,跑在不同的端口号上,不同于Web Server。
    • 你需要配置好你的Web Server,让它把网络请求发送到WSGI服务器。等WSGI服务器处理完请求,再把Response返回到Web Server。

    如果你是用的是目前流行的框架,比如Django和Flask。那么你不需要担心WSGI实现的问题,这些框架都已经实现好了。同样地,如果你使用类似于Gunicorn和uWSGI之一类的WSGI服务器,你也不需要担心WSGI的实现问题。它们都是基于WSGI实现的。你只需要进行合理地配置,让它们知道如何与彼此之间进行通信就可以了。

    Example web server configuration

    一个Web Server的配置中会写明了什么网络请求应该被转送到WSGI 服务器。

    例如,一个Nginx的web服务器写明了,Nginx需要处理静态资源,比如images,JavaScript和CSS文件。这些文件一般位于/static目录下面。除此之外的请求都转交给WSGI位于8000端口的服务器。

    # this specifies that there is a WSGI server running on port 8000
    upstream app_server_djangoapp {
        server localhost:8000 fail_timeout=0;
    }
    
    # Nginx is set up to run on the standard HTTP port and listen for requests
    server {
      listen 80;
    
      # nginx should serve up static files and never send to the WSGI server
      location /static {
        autoindex on;
        alias /srv/www/assets;
      }
    
      # requests that do not fall under /static are passed on to the WSGI
      # server that was specified above running on port 8000
      location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    
        if (!-f $request_filename) {
          proxy_pass http://app_server_djangoapp;
          break;
        }
      }
    }
    

    WSGI Servers

    目前比较流向的WSGI服务器有:

    • Green Unicorn is a pre-fork worker model based server ported from the Ruby Unicorn project.

    • uWSGI is gaining steam as a highly-performant WSGI server implementation.

    • mod_wsgi is an Apache module implementing the WSGI specification.

    • CherryPy is a pure Python web server that also functions as a WSGI server.

    相关文章

      网友评论

          本文标题:浅析WSGI

          本文链接:https://www.haomeiwen.com/subject/vhchmctx.html