Web Server Gateway InterfaceThe Web Server Gateway Interface (WSGI, pronounced whiskey[1][2] or WIZ-ghee[3]) 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.[4] WSGI was originally specified as PEP-333 in 2003.[5] PEP-3333, published in 2010, updates the specification for Python 3. BackgroundIn 2003, Python web frameworks were typically written against only CGI, FastCGI, mod_python, or some other custom API of a specific web server.[6] To quote PEP 333:
WSGI was thus created as an implementation-neutral interface between web servers and web applications or frameworks to promote common ground for portable web application development.[4] Specification overviewThe WSGI has two sides:
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 middlewareA 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.[7] A middleware component can perform such functions as:[7]
ExamplesExample applicationA 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!\n'
Where:
Example of calling an applicationA 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.[10] Details of how to construct the 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
Numerous web frameworks support WSGI:
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),[26] Nginx Unit (using the Python language module),[27] and Microsoft IIS (using WFastCGI,[28] isapi-wsgi,[29] PyISAPIe,[30] or an ASP gateway). See also
References
External links
|
Portal di Ensiklopedia Dunia