<div>You could probably generalize and simplify by using a collections.deque of hosts/ports and using the rotate() method before each reactor.connectTCP.  Also, no need for multiple reactor imports---one at the top of the code is fine.  Note that if the connection is &quot;lost&quot; in a non-clean fashion, you may also want to reconnect.  IIUC, &quot;fail&quot; only handles the case that no connection is made (protocol is never created).</div>
<div><br></div><div><div>import collections</div><div>from twisted.internet import reactor</div><div><br></div><div>class MyClientFactory(object):</div><div>    protocol = MyClientProtocol</div><div>    def __init__(self, hosts):</div>
<div>        self.hosts = collections.deque(hosts)</div><div>        reactor.callWhenRunning(reactor.connectTCP, self.hosts[0][0], self.hosts[0][1], self)</div><div>        self.hosts.rotate(1)</div><div>    def clientConnectionFailed(self, connector, reason):</div>
<div>        reactor.callLater(2.0, connectTCP, self.hosts[0][0], self.hosts[0][1], self)</div><div>        self.hosts.rotate(1)</div><div><br></div><div>factory = MyClientFactory([(&#39;host1&#39;, port1), (&#39;host2&#39;, port2), ...])</div>
<div>reactor.run()</div></div><div><br></div><div>Cheers,</div><div><br>Jason<br><div><br></div></div><div><div class="gmail_quote">On Tue, Jan 11, 2011 at 5:17 AM,  <span dir="ltr">&lt;<a href="mailto:benjamin.bertrand@lfv.se">benjamin.bertrand@lfv.se</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
<br>
I&#39;m new to twisted and I have started to write a new protocol with a TCP<br>
client and server.<br>
In my protocol, a client should be able to connect to 2 servers<br>
(master/slave node - only the master accepts connection).<br>
The client should try to connect to server1. If it fails, try to connect<br>
to server2 (after a specific timeout). If that fails, try server1...<br>
I came up with a solution (see below).<br>
As I&#39;m new to twisted and I haven&#39;t seen anything like that in the<br>
examples, I&#39;d like to check if that&#39;s a proper way to do it.<br>
Any comments is welcome.<br>
<br>
Thanks<br>
<br>
Benjamin<br>
<br>
***********************************************<br>
class MyClientFactory(ClientFactory):<br>
<br>
    protocol = MyClientProtocol<br>
<br>
    def __init__(self, host2=None):<br>
        self.host1 = None<br>
        self.host2 = host2<br>
<br>
    def clientConnectionFailed(self, connector, reason):<br>
        from twisted.internet import reactor<br>
        if self.host2 is None:<br>
            # host2 is not defined, reconnect to host1<br>
            reactor.callLater(2.0, connector.connect)<br>
        else:<br>
            destination = connector.getDestination()<br>
            if self.host1 is None:<br>
                # First connection failed, initialize host1, and try<br>
host2<br>
                self.host1 = destination.host<br>
                host = self.host2<br>
            elif destination.host == self.host1:<br>
                # Connection to host1 failed, try host2<br>
                host = self.host2<br>
            else:<br>
                # Connection to host2 failed, try host1<br>
                host = self.host1<br>
            reactor.callLater(2.0, reactor.connectTCP, host,<br>
destination.port, self)<br>
<br>
<br>
factory = MyClientFactory(server2)<br>
from twisted.internet import reactor<br>
reactor.connectTCP(server1, 8010, factory)<br>
reactor.run()<br>
***********************************************<br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Jason Rennie<br>Research Scientist, ITA Software<br>617-714-2645<br><a href="http://www.itasoftware.com/">http://www.itasoftware.com/</a><br><br>
</div>