2010-10-04 51 views

回答

19

我从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) 

诀窍是使用嵌套方法。