What is the appropriate way to intercept WSGI start_response?

后端 未结 1 794
难免孤独
难免孤独 2021-01-13 11:59

I have WSGI middleware that needs to capture the HTTP status (e.g. 200 OK) that inner layers of middleware return by calling start_response. Curre

相关标签:
1条回答
  • 2021-01-13 12:37

    You can assign the status as an injected field of local_start function itself rather than using status list. I used something similar, works fine:

    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
    
    0 讨论(0)
提交回复
热议问题