Pyramid添加Middleware的方法实例
假设我们要添加一个我们自己的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')
相关推荐
liuxudong00 2020-11-19
wwzaqw 2020-11-11
lihaoxiang 2020-11-05
CrossingX 2020-11-04
xuegangic 2020-10-17
86417413 2020-11-25
83206733 2020-11-19
86276537 2020-11-19
83266337 2020-11-19
86256434 2020-11-17
zhouboxiao 2020-11-16
rise 2020-11-22
sssdssxss 2020-11-20
windle 2020-11-10
孙雪峰 2020-10-30
85477104 2020-11-17
xfcyhades 2020-11-20
cheidou 2020-11-19