Python twisted中ConnectionLost异常的处理
我无法处理无连接异常。 我所拥有的简单例子。首先,我要建立到jabber服务器的连接并ping它。我用沃克尔图书馆。然后我将errback添加到发送ping的方法中。在errback中,我处理ConnectionLost错误。之后,我关闭了互联网连接。但我看不出是否处理了这个连接。我关闭应用程序中的连接,并调用所有异常处理程序 平进展顺利Python twisted中ConnectionLost异常的处理,python,error-handling,twisted,Python,Error Handling,Twisted,我无法处理无连接异常。 我所拥有的简单例子。首先,我要建立到jabber服务器的连接并ping它。我用沃克尔图书馆。然后我将errback添加到发送ping的方法中。在errback中,我处理ConnectionLost错误。之后,我关闭了互联网连接。但我看不出是否处理了这个连接。我关闭应用程序中的连接,并调用所有异常处理程序 平进展顺利 [XmlStream,client] Ping to JID(u'jabber.ru') started at HivemindPingClientProto
[XmlStream,client] Ping to JID(u'jabber.ru') started at HivemindPingClientProtocol
[-] SEND: «iq to='jabber.ru' type='get' id='H_3'>/>»
[XmlStream,client] RECV: "/><feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
不会调用ConnectionLost的处理程序。“在HivemindXMPPClient关闭的流”在StreamManager中以_disconnected方法打印
[-] Protocol stopped
[-] Protocol closed
[-] Transport stopped
[XmlStream,client] Stream closed at HivemindXMPPClient
所有异常都在关闭流后处理
[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion.
[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion.]
[XmlStream,client] Connection lost with [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.]
[XmlStream,client] Stopping factory <hivemind.network.network_core.HivemindXmlStreamFactory object at 0xa2e904c>>
协议包括:
clientConnectionFailed(self, connector, reason)
clientConnectionLost(self, connector, reason)
您可以同时覆盖和调用
PingClientProtocol.clientConnectionFailed
和PingClientProtocol.clientConnectionLost
假设PingClientProtocol以某种方式从Protocol继承,谢谢,但实际上PingClientProtocol是从XMPPHandler继承的,而不是从Protocol继承的。这些方法属于ClientFactory。我不知道如何执行你说的话。你能举个简单的例子吗?具有类似的方法,因此您可以使用:def connectionLost(reason):#在这里完成您的工作#然后调用超级方法XMPPHandler.connectionLost(self,reason)
import sys
from twisted.python import log
from twisted.words.protocols import jabber
from twisted.internet.error import ConnectionLost
from wokkel.client import XMPPClient
from wokkel.ping import PingClientProtocol
from twisted.internet.task import LoopingCall
JID = unicode('YOUR@JABBER.ID')
PASSWORD = 'PASSWORD'
INTERVAL = 3
class SpecialPingClientProtocol(PingClientProtocol):
def __init__(self, entity, interval):
self.__entity = jabber.jid.internJID(entity)
self.__interval = interval
self.__pingLoop = None
def _onError(self, failure):
log.msg('Failure %s at %s' % (failure, self.__class__.__name__))
error = failure.trap(jabber.error.StanzaError, ConnectionLost)
if error == jabber.error.StanzaError:
if failure.value.condition == 'feature-not-implemented':
return None
elif error == ConnectionLost:
# Do some beautiful things
log.msg('Connection is lost. I want to reconnect NOW')
return failure
def _sendPing(self):
defer = self.ping(self.__entity)
defer.addErrback(self._onError)
def stopPing(self):
log.msg('Ping to %s stopped at %s' % (self.__entity, self.__class__.__name__))
if self.__pingLoop is not None and self.__pingLoop.running:
self.__pingLoop.stop()
self.__pingLoop = None
def startPing(self):
log.msg('Ping to %s started at %s ' % (self.__entity, self.__class__.__name__))
self.__pingLoop = LoopingCall(self._sendPing)
self.__pingLoop.start(self.__interval, now = False)
def main():
log.startLogging(sys.stdout)
transport = XMPPClient(jabber.jid.internJID(JID), PASSWORD)
transport.logTraffic = True
pinger = SpecialPingClientProtocol(JID, INTERVAL)
pinger.setHandlerParent(transport)
transport.startService()
pinger.startPing()
reactor.run()
if __name__ == '__main__':
from twisted.internet import reactor
main()
clientConnectionFailed(self, connector, reason)
clientConnectionLost(self, connector, reason)