如何在Python Twisted中关闭和启动服务器?
我的客户端(基于twisted)应该在连接丢失时自动重新连接到服务器,我需要对该功能进行测试,下面是我的测试方法,@todo注释非常清楚预期的行为:如何在Python Twisted中关闭和启动服务器?,python,twisted,trial,Python,Twisted,Trial,我的客户端(基于twisted)应该在连接丢失时自动重新连接到服务器,我需要对该功能进行测试,下面是我的测试方法,@todo注释非常清楚预期的行为: @defer.inlineCallbacks def test_reconnect_on_connection_loss(self): client = SMPPClientFactory(self.config) client.reConnect = mock.Mock(wraps=client.reConnect) #
@defer.inlineCallbacks
def test_reconnect_on_connection_loss(self):
client = SMPPClientFactory(self.config)
client.reConnect = mock.Mock(wraps=client.reConnect)
# Connect
smpp = yield client.connect()
# Bind
yield smpp.bindAsTransmitter()
# @todo: A connection loss is expected here
# the client is supposed to try reconnections
# for a while, the server then shall start
# again and the client will get connected.
# Unbind & Disconnect
yield smpp.unbindAndDisconnect()
##############
# Assertions :
# Protocol verification
self.assertNotEqual(0, client.reConnect.call_count)
在服务器端,我在收到bindAsTransmitter请求后尝试中止连接:
class LooseConnectionOnBindSMSC(SMSC):
def handleBindAsTransmitter(self, reqPDU):
self.sendSuccessResponse(reqPDU)
# Connection is aborted here:
self.transport.abortConnection()
连接成功中止,我的客户端开始尝试按预期重新连接,但它无法再次启动我的服务器。您的服务器仍在运行(从您问题中的代码可以看出)。关闭与客户端的一个连接不会阻止服务器接受新连接
停止侦听端口侦听的方法是使用
port.stopListening()
(请注意,它返回一个延迟的
)。您可以使用另一个reactor.listenTCP
(或您第一次开始侦听的任何API)调用再次开始侦听端口。Jean paul,是的,当我中止连接服务器端时,服务器仍在侦听。。。我需要一种方法停止听几秒钟,然后重新开始。对不起,我不明白你的意思。