- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有 WSGI 中间件需要捕获中间件内层通过调用 start_response
返回的 HTTP 状态(例如 200 OK
)。目前我正在做以下事情,但滥用列表对我来说似乎不是“正确”的解决方案:
class TransactionalMiddlewareInterface(object): def __init__(self, application, **config): self.application = application self.config = config def __call__(self, environ, start_response): status = [] def local_start(stat_str, headers=[]): status.append(int(stat_str.split(' ')[0])) return start_response(stat_str, headers) try: result = self.application(environ, local_start) finally: status = status[0] if status else 0 if status > 199 and status
列表滥用的原因是我无法从一个完全包含的函数中为父 namespace 分配一个新值。
最佳答案
您可以将状态分配为 local_start
函数本身的注入(inject)字段,而不是使用 status
列表。我使用了类似的东西,效果很好:
class TransactionalMiddlewareInterface(object):
def __init__(self, application, **config):
self.application = application
self.config = config
def __call__(self, environ, start_response):
def local_start(stat_str, headers=[]):
local_start.status = int(stat_str.split(' ')[0])
return start_response(stat_str, headers)
try:
result = self.application(environ, local_start)
finally:
if local_start.status and local_start.status > 199:
pass
关于python - 拦截 WSGI start_response 的合适方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2384908/
你能提供一个现实生活中的例子吗 WSGI start_response功能? (网络服务器为 wsgi 应用程序提供该功能) 我不明白引入start_response的目的。 (我读过大约 10 篇关
我编辑了我的问题,因为我意识到我正在结合两个概念(WSGIHandler 和中间件)我不应该在这个问题中结合起来。 我试图对 WSGIHandler 的作用有一些基本的了解。 django.core.
我想做以下事情: r = requests.post('https://foo.com/test', data=json.dumps(fields), headers=headers) if r.st
我有 WSGI 中间件需要捕获中间件内层通过调用 start_response 返回的 HTTP 状态(例如 200 OK)。目前我正在做以下事情,但滥用列表对我来说似乎不是“正确”的解决方案: cl
非 WSGI 方式: def my_view(request): request.start_response('200 OK') request.send_header('Conte
我是一名优秀的程序员,十分优秀!