开发者

What is the appropriate way to intercept WSGI start_response?

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. Currently I'm doing the following, but abusing a list doesn't seem to be the “right” solution to me:

class TransactionalMiddlewareInterface(object):
    def __init__(self, application, **config):
        self.app开发者_如何转开发lication = 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 

The reason for the list abuse is that I can not assign a new value to the parent namespace from within a wholly contained function.


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


Just use the a simple variable with a nonlocal keywork.

class TransactionalMiddlewareInterface(object):
    def __init__(self, application, **config):
        self.application = application
        self.config = config

    def __call__(self, environ, start_response):
        status = 0

        def local_start(stat_str, headers=[]):
            nonlocal status
            status = int(stat_str.split(' ')[0])
            return start_response(stat_str, headers)

        try:
            result = self.application(environ, local_start)

        finally:
            if status > 199 and status 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜