質問

私はニーズが200 OKを呼び出すことにより、ミドルウェア・リターンの内層というHTTPステータス(例えばstart_response)をキャプチャすることWSGIミドルウェアを持っています。現在、私は次のことをやっているが、リストを乱用することは私には「正しい」解決策ではないようです。

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 

リスト虐待の理由は、私は完全に含まれている関数内から親の名前空間に新しい値を割り当てることができないということです。

役に立ちましたか?

解決

あなたはlocal_start機能自体ではなく、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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top