Pyramid添加Middleware的方法实例
时间:2022-11-25 18:50:34
假设我们要添加一个我们自己的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')
标签:Pyramid,Middleware,python
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
请站在用户的角度上说话
2009-05-12 12:03:00
python实现矩阵打印
2023-04-27 06:34:30
![](https://img.aspxhome.com/file/2023/5/75255_0s.jpg)
教程:打造SQL Server2000的安全策略
2008-12-23 15:52:00
利用Python实现最小二乘法与梯度下降算法
2021-07-19 03:59:49
![](https://img.aspxhome.com/file/2023/9/82599_0s.png)
Oracle 随机数
2009-06-04 10:01:00
使用 Python 处理3万多条数据只要几秒钟
2023-08-03 15:21:06
![](https://img.aspxhome.com/file/2023/6/60296_0s.jpg)
asp中如何过滤到单引号
2009-07-05 18:38:00
防止网上重复投票的方法
2009-06-01 12:31:00
pip安装tensorflow的坑的解决
2022-02-07 13:26:32
![](https://img.aspxhome.com/file/2023/4/107934_0s.png)
详解利用OpenCV提取图像中的矩形区域(PPT屏幕等)
2022-06-23 16:42:41
![](https://img.aspxhome.com/file/2023/0/82600_0s.jpg)
Python面试之os.system()和os.popen()的区别详析
2023-09-14 18:15:39
python实现最速下降法
2023-08-10 18:19:51
![](https://img.aspxhome.com/file/2023/9/62259_0s.jpg)
Python监听剪切板实现方法代码实例
2023-11-10 13:48:32
Python虚拟机栈帧对象及获取源码学习
2022-08-26 07:15:30
![](https://img.aspxhome.com/file/2023/9/113019_0s.jpg)
python实现将视频按帧读取到自定义目录
2023-07-15 01:24:11
Python退火算法在高次方程的应用
2023-09-07 22:39:30
![](https://img.aspxhome.com/file/2023/6/128466_0s.png)
python groupby 函数 as_index详解
2023-09-13 21:04:07
python使用ddt过程中遇到的问题及解决方案【推荐】
2021-08-25 09:20:44
![](https://img.aspxhome.com/file/2023/7/112967_0s.jpg)
Python实现GUI学生信息管理系统
2022-03-19 19:35:36
![](https://img.aspxhome.com/file/2023/6/82796_0s.png)
python elasticsearch环境搭建详解
2021-03-31 03:50:42