我有一个扭曲的 ReconnectingClientFactory,我可以通过该工厂成功连接到给定的 ip 和端口。而且效果很好。
reactor.connectTCP(ip, 端口, 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()
我还没有实际测试过它,但至少它应该为您提供一个良好的起点。祝你好运。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)