PylonsHQ.

Layout: Fixed-width

Adding your own middleware

Skip to end of metadata
Go to start of metadata

Note: additional info on WSGI and middleware in Pylons can be found in the document Web Server Gateway Interface Support.

Adding your own middleware

As Ian Bicking (creator of "Paste") said: "Middleware is where people get a little intimidated by WSGI and Paste." But if you understand the very basics of WSGI and how the actual HTTP request is passed around it is not that scaring any more. A good introduction is given by Ian's A Do-It-Yourself Framework article.

A very simple piece of middleware:

1
2
3
4
5
6
class MyMiddleware(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        return ['This request passed through MyMiddleware'] + self.app(environ, start_response)

This will prepend all pages that are sent to the browser with the sentence "This request passed through MyMiddleware". To plug into your application just add the following line in your middleware.py where it reads "Put your own middleware here":

1
app = MyMiddleware(app)

(Re-)Start your application and request a URL. You should see your sentence at the top.

Of course this is just a Hello-World-style example. There are much better uses for middleware. Check out the section on middleware in Ian's article for more information.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Powered by Pylons - Contact Administrators