首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

twisted框架搭建的服务器 怎么实现 关闭套接口和端口复用

2013-01-06 
twisted框架搭建的服务器 如何实现 关闭套接口和端口复用利用 python 是用twisted框架进行 网络编程 问题r

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()

控制台输出
2012-11-30 15:15:39+0800 [-] Log opened.
2012-11-30 15:15:39+0800 [-] ServerFactory starting on 8001
2012-11-30 15:15:39+0800 [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0x01E16A30>
2012-11-30 15:15:39+0800 [-] ServerFactory starting on 8002
2012-11-30 15:15:39+0800 [-] (TCP Port 8001 Closed)
2012-11-30 15:15:39+0800 [-] ServerFactory starting on 8001

热点排行