Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>WSGI is <a href="http://www.python.org/dev/peps/pep-0333/" rel="nofollow">PEP 333</a> (and <a href="http://www.python.org/dev/peps/pep-3333/" rel="nofollow">PEP3333</a> for Python 3), a.k.a. Web Server Gateway Interface. It has three parts, but the part you're interested in is how you write a WSGI application. And WSGI app is a callable object that takes two arguments and returns an iterable object (or is a generator).</p> <pre><code># this is my_app module def app(environ, start_response): # environ is dict-like object containing the WSGI environment # refer to the PEP for details # start_response is a callable that, well, starts the response headers = [('Content-Type', 'text/plain; charset=utf-8')] start_response('200 OK', headers) return ["I'm a WSGI application.\n"] </code></pre> <p>To run the application, you need another part of WSGI, which is gateway. In the standard library you'll find <code>wsgiref</code> package. It contains a CGI gateway:</p> <pre><code>#!/usr/bin/python # this is a CGI script that runs a WSGI application inside CGI handler from wsgiref.handlers import CGIHandler from my_app import app CGIHandler().run(app) </code></pre> <p>And also a simple HTTP server for development:</p> <pre><code>from wsgiref.simple_server import make_server from my_app import app httpd = make_server('localhost', 8000, app) httpd.serve_forever() </code></pre> <p>As you can see, WSGI allows you to reuse your application in different environments — CGI, SCGI, FastCGI, mod_wsgi, mod_python, etc., without actually rewriting it.</p> <p>The last part of WSGI is middleware — basically, it's a concept that allows you to combine different WSGI applications. It forms sort of a sandwich — request flows from the top (the gateway) to the bottom (which is usually your application), with some intermediate layers in between, that might implement stuff like database connection pooling or sessions. <code>wsgiref</code> contains one such middleware — <code>wsgiref.validate.validator</code>, which checks whether layers below and above it conforms to the rules of WSGI spec.</p> <p>And that's basically it. Now go use a framework.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload