扭曲:将ClientFactory连接重新连接到其他服务器
问题内容:
我有一个扭曲的ReconnectingClientFactory,我可以与此工厂成功连接到给定的ip和端口对。而且效果很好。
reaction.connectTCP(ip,port,myHandsomeReconnectingClientFactory)
在这种情况下,当服务器消失时,myHandsomeReconnectingClientFactory会尝试连接相同的IP和端口(如预期的那样)。
我的目标是,当给定IP和端口对上的服务器消失时,连接到备份服务器(具有不同的IP和端口)。
任何有关如何实现这一目标的想法/评论将不胜感激。
问题答案:
尝试尝试类似的方法:
class myHandsomeReconnectingClientFactory(protocol.ReconnectingClientFactory):
def __init_(self, hosts):
# hosts should be a list of tuples (host, port)
self._hosts = hosts
def clientConnectionFailed(self, connector, reason):
if self.continueTrying:
self._try_next_host(connector)
def clientConnectionLost(self, connector, unused_reason):
if self.continueTrying:
self._try_next_host(connector)
def _try_next_host(self, connector):
# round robing of servers
to_try = self._hosts.pop(0)
self._hosts.append(to_try)
connector.host, connector.port = to_try
self.connector = connector
self.retry()
我尚未实际测试过,但至少它应该为您提供一个良好的起点。祝你好运。