twisted框架搭建的服务器 如何实现 关闭套接口和端口复用
利用 python 是用twisted框架进行 网络编程 问题
reactor.listenTCP(dic['serverPort'], factory) 这样来 添加一个 监听套接口
然后 reactor.run()但是 我监听了n个端口
并不想 reactor.stop() 。
1 就是在启动状态下 判断条件 废除监听的某个 端口 但是不知道 twisted用哪个方法 可以废除 (如果twisted里面没有这个方法 那用什么方法 可 关闭此监听)
2 twisted如何 可以是 端口复用
[解决办法]
stopListening
下面
If it does not complete immediately, will return Deferred that fires upon completion.
如果不是立即完成,就返回一个defer
from twisted.internet import protocol
log.startLogging(sys.stdout)
factory = protocol.ServerFactory()
port1=reactor.listenTCP(8001, factory)
port2=reactor.listenTCP(8002, factory)
d=port1.stopListening()#sotp 8001
d.addCallback(lambda result:reactor.listenTCP(8001, factory))
reactor.run()