2

python twisted モジュールを使用してヘッダーをインターセプトしています。次のスクリプトがあります。

from twisted.web import proxy, http
from twisted.internet import reactor

g_port = 8080 #port for listening
g_request = () # action, params, method

class akaProxy(proxy.Proxy):

    def dataReceived(self, data):

        action = None
        params = None
        method = None
        global g_request

        headers = data.split("\n")
        request = headers[0].split(" ")
        params = headers[len(headers)-1]

        method = request[0].lower()
        action = request[1].lower()

        print method, action

        return proxy.Proxy.dataReceived(self, data)

class ProxyFactory(http.HTTPFactory):
    protocol = akaProxy


factory = ProxyFactory()
reactor.listenTCP(g_port, factory)
reactor.run()

Web ブラウザー (この URL )に localproxy を使用して実行すると、次のエラーが表示されます。

Unhandled Error
Traceback (most recent call last):
  File "test2.py", line 33, in <module>
    reactor.run()
  File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1169, in run
    self.mainLoop()
  File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1181, in mainLoop
    self.doIteration(t)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/pollreactor.py", line 167, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 599, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, inRead)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 260, in _disconnectSelectable
    selectable.readConnectionLost(f)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 257, in readConnectionLost
    self.connectionLost(reason)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 433, in connectionLost
    Connection.connectionLost(self, reason)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 277, in connectionLost
    protocol.connectionLost(reason)
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 455, in connectionLost
    self.handleResponseEnd()
  File "/usr/lib/python2.7/dist-packages/twisted/web/proxy.py", line 88, in handleResponseEnd
    self.father.finish()
  File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 866, in finish
    "Request.finish called on a request after its connection was lost; "
exceptions.RuntimeError: Request.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this.

しかし、それは引き続き機能します。誰かがエラーを説明できますか?どうすれば修正できますか?

4

1 に答える 1

1

このメッセージは、次のような状況で表示されます: http リクエストを処理するコードがありfinish、http 接続が失われるまで (何らかの理由で) リクエストのメソッドが呼び出されませんでした (つまり、リクエストを送信する方法がなくなりました)。結果はリクエスタに送信されます)。

あなたはそれが起こることを気にしないかもしれません。そうでない場合は、リクエストの属性が trueRequest.finishのときにメソッドが呼び出されないように、コードで何かを行う必要があります。_disconnectedそれ、または単にエラーを無視します。

于 2012-05-24T16:25:51.327 に答える