重新连接到扭曲的其他地址? [英] Reconnect to different address in twisted?

查看:74
本文介绍了重新连接到扭曲的其他地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器,该服务器可以将备份服务器的地址发送给我的客户端,以防万一它发生故障.在服务器端,备份服务器正在运行.在客户端,一旦连接断开,我将无法使客户端连接到备份服务器.我知道我必须将连接逻辑添加到twisted.internet.protcol.Protocol

I have a server that sends my client the address of a backup server in case it goes down. On the server side the backup server is running. On the client side once the connection is lost I am not able to make the client to connect to the backup server. I know that I have to add the connection logic to the following callback in twisted.internet.protcol.Protocol

class MyProtocol(Protocol):
    def connectionLost(self, reason):
        print 'Connection Lost'
        print 'Trying to reconnect'
        # How do I reconnect to another address say at localhost:8001

f = Factory()
f.protocol = MyProtocol
reactor.connectTCP("localhost", 8000, f)
reactor.run()

如果localhost:8000上的服务器停止,它将触发connectionLost(..)方法.在这种方法中,我想将逻辑连接到备份主机(在本例中为localhost:8001),但可以是任意的.我该怎么办 这个吗?

If the server on localhost:8000 stopped it will trigger the connectionLost(..) method. In this method I want to put the logic to connect to the backup host which in this case is say localhost:8001, but could be anything arbitrary. How do I do this?

修改: 我不想使用ReconnectingClientFactory

I want to do this without using ReconnectingClientFactory

推荐答案

class MyProtocol(Protocol):
    def connectionLost(self, reason):
        print 'Connection Lost'
        print 'Trying to reconnect'
        reactor.connectTCP(
            "localhost", 8001, Factory.forProtocol(MyProtocol),
        )

reactor.connectTCP("localhost", 8000, Factory.forProtocol(MyProtocol))
reactor.run()

这篇关于重新连接到扭曲的其他地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆