I have an almost complete simple web app written as a Python CGI script. I would like to change it to use WSGI, but I can’t find documentation that helps me make sense of what WSGI actually is (one only repeatedly finds calls with start_response etc. but there doesn’t seem to be much explanation fo rwhat these calls actually do). Can someone point me to a good explanation, plus a how-to on using WSGI?
Edit: Should add that I’ve seen this question but the answers still don’t seem to tell one how to use WSGI in a direct script (as opposed to in a framework).
WSGI is PEP 333 (and PEP3333 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).
To run the application, you need another part of WSGI, which is gateway. In the standard library you’ll find
wsgirefpackage. It contains a CGI gateway:And also a simple HTTP server for development:
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.
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.
wsgirefcontains one such middleware —wsgiref.validate.validator, which checks whether layers below and above it conforms to the rules of WSGI spec.And that’s basically it. Now go use a framework.