Twisted HTTP代理通道在LostConnection时设置为None,但尚未调用loseConnection

1 投票
1 回答
825 浏览
提问于 2025-04-16 05:20

我正在写一个代理程序,用来把HTTP的GET请求转换成POST请求(因为很多Python的媒体播放器只支持GET请求)。现在我在做缓存的功能,这样我就只需要下载一次一个网址。我把很多代码从父类移动到了子类,并进行了修改,这样我就可以把相同的内容发送到多个服务器连接上(比如,如果建立了一个服务器连接,我就会把它附加到一个下载实例上,如果有的话)。

总的来说,这个程序大部分都能正常工作,但最后一部分出现了问题,奇怪的是在我调用loseConnection之前,HTTP通道被设置成了None(而且没有任何错误,比如网络断开等)。这导致了一个错误,提示我调用loseConnection的对象是None,而None是没有transport属性的。

这是代码

    self.connectionDone = True
    print self.producers
    for p in self.producers:
        print p
        print p.channel
        print dir(p)
        p.channel.transport.loseConnection()

    self.transport.loseConnection()

还有self.channel被设置为None的追踪信息(在每个setattr调用时使用traceback模块的print_stack)

注意:看看最后几行,我觉得问题出在这里。

   File "Sharky.py", line 581, in <module>
     reactor.run()
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\wxreactor.py", line 145, in run
     self.wxapp.MainLoop()
   File "C:\Program Files\Python 2.6.2\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 8007, in MainLoop
     wx.PyApp.MainLoop(self)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7303, in MainLoop
     return _core_.PyApp_MainLoop(*args, **kwargs)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 14640, in <lambda>
     lambda event: event.callable(*event.args, **event.kw) )
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 243, in _interleave
     getattr(self, '_process_' + msg)(*args)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 209, in _process_Notify
     _logrun(selectable, _drdw, selectable, method, dct)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", line 84, in callWithLogger
     return callWithContext({"system": lp}, func, *args, **kw)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", line 69, in callWithContext
     return context.call({ILogContext: newCtx}, func, *args, **kw)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.py", line 59, in callWithContext
     return self.currentContext().callWithContext(ctx, func, *args, **kw)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.py", line 37, in callWithContext
     return func(*args,**kw)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 303, in _doReadOrWrite
     self._disconnectSelectable(selectable, why, method == "doRead")
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\posixbase.py", line 253, in _disconnectSelectable
     selectable.connectionLost(f)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\tcp.py", line 519, in connectionLost
     protocol.connectionLost(reason)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", line 1725, in connectionLost
     request.connectionLost(reason)
   File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", line 1287, in connectionLost
     self.channel = None
   File "C:\Documents and Settings\Admin\My Documents\Mercurial\sharky\ProxyServer.py", line 172, in __setattr__
     traceback.print_stack()

这是我遇到的None属性异常

Traceback (most recent call last):
  File "C:\Program Files\Python 2.6.2\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7303, in MainLoop
    return _core_.PyApp_MainLoop(*args, **kwargs)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 14640, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 243, in _interleave
    getattr(self, '_process_' + msg)(*args)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 209, in _process_Notify
    _logrun(selectable, _drdw, selectable, method, dct)
--- <exception caught here> ---
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.py", line 59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\python\context.py", line 37, in callWithContext
    return func(*args,**kw)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\_threadedselect.py", line 303, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, method == "doRead")
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\posixbase.py", line 253, in _disconnectSelectable
    selectable.connectionLost(f)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\tcp.py", line 677, in connectionLost
    Connection.connectionLost(self, reason)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\internet\tcp.py", line 519, in connectionLost
    protocol.connectionLost(reason)
  File "C:\Program Files\Python 2.6.2\lib\site-packages\twisted\web\http.py", line 489, in connectionLost
    self.handleResponseEnd()
  File "C:\Documents and Settings\Admin\My Documents\Mercurial\sharky\ProxyServer.py", line 103, in handleResponseEnd
    p.channel.transport.loseConnection()
exceptions.AttributeError: 'NoneType' object has no attribute 'transport'

1 个回答

1

连接可能会在没有网络断开的情况下丢失。只要连接的一方调用了shutdown()或close(),就会发生这种情况。你有没有考虑过这个问题?即使你已经考虑过,为了让代码正确,它还是需要处理这种可能性,因为这种情况可能在其他时候发生。可以看看Request.notifyFinish。

撰写回答