【问题标题】:twisted http server error扭曲的http服务器错误
【发布时间】:2012-05-22 20:55:59
【问题描述】:

我正在使用 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 浏览器的 localproxy (this URL) 运行它时,给我这个错误:

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.

但它继续工作,任何人都可以解释错误以及如何解决它?

【问题讨论】:

    标签: twisted twisted.web


    【解决方案1】:

    此消息出现在与它描述的情况类似的情况下:您有一些处理 http 请求的代码,并且在 http 连接丢失(意味着存在现在无法将结果发送给请求者)。

    您可能不在乎这种情况。如果你不这样做,那么你应该在你的代码中做一些事情来防止Request.finish 方法在你的请求的_disconnected 属性为真时被调用。那,或者只是忽略错误。

    【讨论】:

    • 所以这不是由服务器中的某些错误引起的问题,而是客户端断开连接太快?我通过在加载 favicon(直接通过 url)时在浏览器上按住 F5 来获得这些异常。服务器是否进入损坏状态,或者它是否从该状态中完全恢复?
    • Twisted 本身不会进入损坏状态,但如果 您的 服务器对连接是否总是成功完成做出无效假设,则可能会进入损坏状态。
    • _disconnected 是 Twisted 的私有变量,应被视为“修复”此问题的 hack。
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2011-04-15
    • 2013-12-05
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多