假设我们要添加一个我们自己的middleware,用来记录每次请求的日志下面就是一个符合规范的middleware, 构造函数中接受一个wsgi app, __call__返回一个wsgi app.
代码如下:
class loggermiddleware(object): ”’wsgi middleware”’
def __init__(self, application):
self.app = application
def __call__(self, environ, start_response):
# write logs
try: return self.app(environ, start_response) except exception, e: # write logs pass finally: # write logs pass
在项目的__init__.py的main函数中, 在config.make_wsgi_app上包上一层我们的middleware:
代码如下:
from pyramid.config import configurator config = configurator() config.scan() app = config.make_wsgi_app()
# put middleware app = loggermiddleware(app)
serve(app, host=’0.0.0.0′)