请输入您要查询的百科知识:

 

词条 Web Server Gateway Interface
释义

  1. Background

  2. Specification overview

  3. WSGI Middleware

  4. Examples

     Example application  Example of calling an application 

  5. WSGI-compatible applications and frameworks

  6. See also

  7. References

  8. External links

{{redirect|WSGI|the radio station in Springfield, Tennessee|WSGI (AM)}}{{TOC right}}

The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language. The current version of WSGI, version 1.0.1, is specified in Python Enhancement Proposal (PEP) 3333.[1]

WSGI was originally specified as PEP-333 in 2003.[2] PEP-3333, published in 2010, updates the specification for Python 3.

Background

In 2003, Python web frameworks were typically written against only CGI, FastCGI, mod_python, or some other custom API of a specific web server.[3] To quote PEP 333:

Python currently boasts a wide variety of web application frameworks, such as Zope, Quixote, Webware, SkunkWeb, PSO, and Twisted Web -- to name just a few. This wide variety of choices can be a problem for new Python users, because generally speaking, their choice of web framework will limit their choice of usable web servers, and vice versa... By contrast, although Java has just as many web application frameworks available, Java's "servlet" API makes it possible for applications written with any Java web application framework to run in any web server that supports the servlet API.

WSGI was thus created as an implementation-agnostic interface between web servers and web applications or frameworks to promote common ground for portable web application development.[1]

Specification overview

The WSGI has two sides:

  • the server/gateway side. This is often a full web server such as Apache or Nginx, or a lightweight application server that can communicate with a webserver, such as [https://www.saddi.com/software/flup/ flup].
  • the application/framework side. This is a Python callable, supplied by the Python program or framework.

Between the server and the application, there may be one or more WSGI middleware components, which implement both sides of the API, typically in Python code.

WSGI does not specify how the Python interpreter should be started, nor how the application object should be loaded or configured, and different frameworks and webservers achieve this in different ways.

WSGI Middleware

A WSGI middleware component is a Python callable that is itself a WSGI application, but may handle requests by delegating to other WSGI applications. These applications can themselves be WSGI middleware components.[4]

A middleware component can perform such functions as:[4]

  • Routing a request to different application objects based on the target URL, after changing the environment variables accordingly.
  • Allowing multiple applications or frameworks to run side-by-side in the same process
  • Load balancing and remote processing, by forwarding requests and responses over a network
  • Performing content post-processing, such as applying XSLT stylesheets

Examples

Example application

A WSGI-compatible "Hello, World" application written in Python:

def application(environ, start_response):

    start_response('200 OK', [('Content-Type', 'text/plain')])    yield b'Hello, World\'

Where:

  • Line 1 defines a callable[5] named application, which takes two parameters, environ and start_response. environ is a dictionary containing CGI environment variables as well as other request parameters and metadata under well-defined keys.[6] start_response is a callable taking two positional parameters, status and response_headers.
  • Line 2 calls start_response, specifying "200 OK" as the HTTP status and a "Content-Type" response header.
  • Line 3 makes the callable into a generator. The body of the response is returned as an iterable of byte strings.

Example of calling an application

A full example of a WSGI network server is outside the scope of this article. Below is a sketch of how one would call a WSGI application and retrieve its HTTP status line, response headers, and response body, as Python objects.[7] Details of how to construct the environ dict have been omitted.

from io import BytesIO

def call_application(app, environ):

    status = None    headers = None    body = BytesIO()        def start_response(rstatus, rheaders):        nonlocal status, headers        status, headers = rstatus, rheaders            app_iter = app(environ, start_response)    try:        for data in app_iter:            assert status is not None and headers is not None, \\                "start_response() was not called"            body.write(data)    finally:        if hasattr(app_iter, 'close'):            app_iter.close()    return status, headers, body.getvalue()

environ = {...} # "environ" dict

status, headers, body = call_application(app, environ)

WSGI-compatible applications and frameworks

{{example farm|section|date=September 2018}}

Numerous web frameworks support WSGI:

{{div col|colwidth=}}
  • [https://github.com/jonashaag/bjoern bjoern]
  • BlueBream
  • bobo[8]
  • Bottle
  • CherryPy
  • Django[9]
  • Eventlet[10]
  • Flask
  • Falcon (web framework) [11]
  • Gevent-FastCGI[12]
  • Google App Engine's webapp2
  • Gunicorn
  • prestans[13]
  • mod_wsgi for use with Apache[14]
  • netius
  • pycnic[15]
  • Paste component WebOb is specifically a WSGI extension. It was adopted by the Pylons project.
  • Pylons
  • Pyramid
  • restlite[16]
  • Tornado
  • Trac
  • TurboGears
  • Uliweb[17]
  • uWSGI
  • Waitress[18]
  • web.py[19]
  • web2py
  • weblayer[20]
  • Werkzeug[21]
  • Radicale[22]
{{div col end}}

Currently wrappers are available for FastCGI, CGI, SCGI, AJP (using flup), twisted.web, Apache (using mod_wsgi or mod_python), Nginx (using ngx_http_uwsgi_module),[23] and Microsoft IIS (using WFastCGI,[24] isapi-wsgi,[25] PyISAPIe,[26] or an ASP gateway).

See also

  • Rack – Ruby web server interface
  • PSGI – Perl Web Server Gateway Interface
  • SCGI – Simple Common Gateway Interface
  • JSGI – JavaScript web server gateway interface

References

1. ^{{cite web|url=https://www.python.org/dev/peps/pep-3333/|title=PEP 3333 - Python Web Server Gateway Interface v1.0.1|last=|first=|date=|website=Python.org|archive-url=|archive-date=|dead-url=|accessdate=2018-04-04}}
2. ^{{Cite web|url=https://www.python.org/dev/peps/pep-0333/|title=PEP 333 -- Python Web Server Gateway Interface v1.0|website=Python.org|language=en|access-date=2018-04-04}}
3. ^{{cite web|url=https://www.python.org/cgi-bin/moinmoin/WebProgramming|title=FrontPage - Python Wiki|date=|website=Python.org|accessdate=2017-01-27}}
4. ^{{Cite web|url=https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides|title=PEP 3333 -- Python Web Server Gateway Interface v1.0.1|website=Python.org|language=en|access-date=2018-04-04}}
5. ^i.e. "a function, method, class, or an instance with a __call__ method"
6. ^{{Cite web|url=https://www.python.org/dev/peps/pep-3333/#environ-variables|title=PEP 3333 -- Python Web Server Gateway Interface v1.0.1|website=Python.org|language=en|access-date=2018-04-04}}
7. ^{{cite web|url=https://www.youtube.com/watch?v=afnDANxsaYo|title=Creating WSGI Middleware - Alan Christopher Thomas - Minted - PythonKC|date=2015-08-28|publisher=YouTube|accessdate=2017-01-27}}
8. ^{{cite web|url=http://bobo.digicool.com |title=プエラリアジェルの効果は? |website=Bobo.digicool.com |date= |accessdate=2017-01-27}}
9. ^{{cite web|url=http://www.djangoproject.com/weblog/2005/jul/18/local_server/ |title=Django without mod_python, and WSGI support | Weblog | Django |website=Djangoproject.com |date=2005-07-18 |accessdate=2017-01-27}}
10. ^{{cite web|author= |url=http://eventlet.net/doc/modules/wsgi.html |title=wsgi – WSGI server — Eventlet 0.20.1 documentation |website=Eventlet.net |date= |accessdate=2017-01-27}}
11. ^{{cite web|url=https://falconframework.org |title=Falcon - Bare-metal web API framework for Python |accessdate=2017-10-22}}
12. ^{{cite web|url=https://pypi.python.org/pypi/gevent-fastcgi |title=gevent-fastcgi 1.0.2.1 : Python Package Index |website=Pypi.python.org |date=2015-12-06 |accessdate=2017-01-27}}
13. ^{{cite web|url=https://github.com/prestans/prestans/ |title=anomaly/prestans: A WSGI compliant REST micro-framework |website=GitHub.com |date= |accessdate=2017-01-27}}
14. ^{{cite web|url=http://code.google.com/p/modwsgi/ |title=Google Code Archive - Long-term storage for Google Code Project Hosting |website=Code.google.com |date= |accessdate=2017-01-27}}
15. ^{{cite web|url=http://pycnic.nullism.com |title=Pycnic Framework |website=Pycnic.nullism.com |date= |accessdate=2017-01-27}}
16. ^{{cite web|url=https://github.com/theintencity/restlite |title=theintencity/restlite: Light-weight RESTful server tools in Python |website=GitHub.com |date= |accessdate=2017-01-27}}
17. ^{{cite web|url=https://github.com/limodou/uliweb |title=limodou/uliweb: Simple and easy use python web framework |website=GitHub.com |date= |accessdate=2017-01-27}}
18. ^{{cite web|url=https://docs.pylonsproject.org/projects/waitress/en/latest/ |title=waitress documentation |website=docs.pylonsproject.org |date= |accessdate=2018-09-26}}
19. ^{{cite web|url=http://webpy.org/ |title=Welcome to |website=Web.py |date=2009-09-11 |accessdate=2017-01-27}}
20. ^{{cite web|url=http://packages.python.org/weblayer |title=weblayer — weblayer v0.4.3 documentation |website=Packages.python.org |date= |accessdate=2017-01-27}}
21. ^{{cite web|url=http://werkzeug.pocoo.org/ |title=Welcome | Werkzeug (The Python WSGI Utility Library) |website=Werkzeug.pocoo.org |date= |accessdate=2017-01-27}}
22. ^{{cite web|url=http://radicale.org/ |title=CalDAV and CardDAV Server - A Simple Calendar and Contact Server |website=Radicale.org |date= |accessdate=2017-01-27}}
23. ^{{cite web|url=http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html |title=Module ngx_http_uwsgi_module |website=Nginx.org |date= |accessdate=2017-01-27}}
24. ^{{cite web|url=https://pytools.codeplex.com/wikipage?title=wfastcgi |title=Python Tools for Visual Studio - Documentation |website=Pytools.codeplex.com |date= |accessdate=2017-01-27}}
25. ^{{cite web|url=http://code.google.com/p/isapi-wsgi/ |title=Google Code Archive - Long-term storage for Google Code Project Hosting |website=Code.google.com |date= |accessdate=2017-01-27}}
26. ^{{cite web|url=http://pyisapie.sourceforge.net/ |title=Python ISAPI Extension for IIS download | SourceForge.net |website=Pyisapie.sourceforge.net |date=2012-04-24 |accessdate=2017-01-27}}

External links

  • [https://www.python.org/dev/peps/pep-0333/ PEP 333 – Python Web Server Gateway Interface]
  • [https://www.python.org/dev/peps/pep-3333/ PEP 3333 – Python Web Server Gateway Interface v1.0.1]
  • WSGI metaframework
  • Comprehensive wiki about everything WSGI
  • WSGI Tutorial
  • [https://docs.python.org/library/wsgiref.html Python standard library module wsgiref]
  • Getting Started with WSGI
  • NWSGI – .NET implementation of the Python WSGI specification for IronPython and IIS
  • Gevent-FastCGI server implemented using gevent coroutine-based networking library
{{Python (programming language)}}{{Web interfaces}}

1 : Python (programming language)

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/21 13:25:12