Twisted: ReconnectingClientFactory 连接到不同的服务器 [英] Twisted: ReconnectingClientFactory connection to different servers
问题描述
我有一个扭曲的 ReconnectingClientFactory 并且我可以成功地连接到给定的 IP 和端口与这个工厂.而且效果很好.
I have a twisted ReconnectingClientFactory and i can successfully connect to given ip and port couple with this factory. And it works well.
reactor.connectTCP(ip, port, myHandsomeReconnectingClientFactory)
reactor.connectTCP(ip, port, myHandsomeReconnectingClientFactory)
在这种情况下,当服务器消失时,myHandsomeReconnectingClientFactory 会尝试连接相同的 ip 和端口(如预期).
In this situation, when the server is gone, myHandsomeReconnectingClientFactory tries to connect same ip and port (as expected).
我的目标是,当服务于给定 ip 和端口对的服务器消失时,连接到备份服务器(具有不同的 ip 和端口).
My goal is, when the server which serves on given ip and port couple is gone, connecting to a backup server (which have different ip and port).
对于如何实现这一目标的任何想法/意见将不胜感激.
Any ideas/comments on how to achieve this goal will be appreciated.
推荐答案
Id 尝试类似:
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()
我还没有真正测试过它,但至少它应该给你一个很好的起点.好屌.
I haven't actually tested it, but at least it should give you a good starting point. Good uck.
这篇关于Twisted: ReconnectingClientFactory 连接到不同的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!