WSGI Middleware
WSGI Middleware
In a previous tutorial we just wrote a basic ‘Hello World’ application in WSGI. At the end of you might, rightly, have been wondering what’s the point of WSGI — after all you could have written that ‘Hello World’ app using plain CGI (or anything else for that matter). In this tutorial we are going to start answering that question by taking a look at WSGI middleware and write a simple piece of middleware ourselves.
A Simple Example
Here a simple piece of middleware that adds authentication based on the remote address of the client (this tutorial and its code is available in raw form at http://www.rufuspollock.org/code/wsgi/):
from wsgiref.simple_server import make_server, demo_app
class AuthenticationMiddleware: """A modified version of an original example at: http://isapi-wsgi.python-hosting.com/wiki/WSGI-Gateway-or-Glue """
def __init__(self, app, allowed_addresses):
"""
@param app: the WSGI app we will that comes after us
@param allowed_addresses: list of remote addresses from which to allow
access
"""
self.app = app
self.allowed_addresses = allowed_addresses
def __call__(self, environ, start_response):
"""The standard WSGI interface"""
addr = environ.get('REMOTE_ADDR','UNKNOWN')
if addr in self.allowed_addresses: # pass through to the next app
return self.app(environ, start_response)
else: # put up a response denied
start_response(
'403 Forbidden', [('Content-type', 'text/html')])
return ['You are forbidden to view this resource']
addresses = [ '127.0.0.1' ] simple_app_with_auth = AuthenticationMiddleware(demo_app, addresses)
if name == 'main':
httpd = make_server('', 8000, simple_app_with_auth)
print "Serving HTTP on port 8000..."
# Respond to requests until process is killed
httpd.serve_forever()
The Basic Idea
As explained in [pep-333] the basic idea of middleware is of something that ‘plays both sides’:
Note that a single object may play the role of a server with respect to some application(s), while also acting as an application with respect to some server(s). Such “middleware” components can perform such functions as:
- Routing a request to different application objects based on the target URL, after rewriting the environ 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 * Perform content postprocessing, such as applying XSL stylesheets
A diagram helps:
WSGI SERVER
V A
V A
| |
| |
+---------------------+
| | | |
| +-------------+ |
| | V A | |
| | +-----+ | |
| | | APP | | |
| | +-----+ | |
| | MIDDLEWARE1 | |
| +-------------+ |
| MIDDLEWARE2 |
+---------------------+
The WSGI Application + Middleware 'Onion'
Basically middleware wraps an underlying wsgi application and then presents itself as the new wsgi application to external callers. In python code the above would like:
core_app = SomeWsgiApplication()
# remember the middleware is itself a wsgi application
wrapped_once = Middleware1(core_app)
# wrap the new wsgi application!
wrapped_twice = Middleware2(wrapped_once)
# alternatively we could do it all in one
wrapped = Middleware2(Middleware1(core_app))
Remarks
Middleware is useful because it dramatically increases the possibilities for using standard web application plumbing — any piece of middleware can now be plugged together very easily with either other middleware or an application.
Middleware is usually one of three types:
- pre-processors
- post-processors
- those that do both (rare)
Examples of pre-processors are:
- Authenticators (including session management)
- Dispatchers including proxies and controllers
Examples of post-processors:
- Applying an XSL style sheet
- Tidying html or providing safe xhtml
In general, pre-processors are a little simpler because they don’t have to deal with the ‘chunking’ aspect of WSGI (a WSGI application return an iterable rather than just a single buffer so as to allow ‘chunking’ of output — this will be useful, for example, when streaming large files, see the’Buffering and Streaming’ section in PEP 333 for more information).
-
Categories
- *nix
- Academic
- Activity Updates
- Books
- Cinema
- Code
- Command Line
- Copyright
- Culture and Society
- Data Digging
- Economics
- EUPD
- External
- Filesharing
- Governance
- Hacks
- Happiness
- Hardware
- History
- Innovation and Intellectual Property
- Intellectual Myths
- Javascript
- Knowledge Systems
- Miscellaneous
- Musings
- Notes
- Open Bibliographic Data
- Open Data
- Open Knowledge Foundation
- Openness
- Own Work
- Papers
- People
- Photos
- Platforms
- Poetry
- Policy
- PSI
- Python
- Quote
- RDF
- Shuttleworth Fellow
- Software
- Sysadmin
- Talks
- Transaction Costs
- Work In Progress
-
Articles
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- June 2004
- May 2004
- March 2004
- October 2003
-
Meta




