Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add http headers in WSGI middleware?

Tags:

python

wsgi

How can http headers be added within a WSGI middleware?

like image 366
deamon Avatar asked Oct 04 '10 21:10

deamon


1 Answers

I've found a nice example from the pylons book.

class Middleware(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):

        def custom_start_response(status, headers, exc_info=None):
            headers.append(('Set-Cookie', "name=value"))
            return start_response(status, headers, exc_info)

        return self.app(environ, custom_start_response)

The trick is to use a nested method.

like image 120
deamon Avatar answered Oct 29 '22 10:10

deamon