vse*_*har 9 python p2p tcp twisted
我正在使用应用程序框架编写一个扭曲的P2P客户端.传入连接的侦听端口将位于随机(OS确定)端口上.但是,我需要一种方法来确定创建它后该端口是什么:
import twisted... etc.
application = service.Application('vmesh')
peerservice = MyPeerService()
servicecollection = service.IServiceCollection(application)
factory = MyPeerFactory(peerservice)
server = internet.TCPServer(0, factory) # listen on random port
listen_port = server.getHost().port # ??? doesn't work...
server.setServiceParent(servicecollection)
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到有关查询由其创建internet.TCPServer()或由其reactor.listenTCP()转发的端口的任何内容.我不能简单地等待连接发生,因为客户端必须宣布其端口才能实现这些连接.
Gly*_*yph 20
listenTCP返回一个IListeningPort,它有一个getHost()方法可以返回一个带有的对象port.例如:
>>> from twisted.internet import reactor
>>> from twisted.internet.protocol import Factory
>>> port = reactor.listenTCP(0, Factory())
>>> port.getHost().port
55791
Run Code Online (Sandbox Code Playgroud)
但是,TCPServer在listenTCP启动之前不会调用privilegedStartService.此外,IListeningPort实际上并未通过公共API公开.所以,你需要自己编写Service.幸运的是,这很容易做到; TCPServer做得不多.您只需要编写一个在开始侦听时立即报告其端口的端口.这是一个例子:
from twisted.internet import reactor
from twisted.application.service import Service
class PortReporter(Service, object):
def __init__(self, factory, reportPort):
self.factory = factory
self.reportPort = reportPort
def privilegedStartService(self):
self.listeningPort = reactor.listenTCP(0, self.factory)
self.reportPort(self.listeningPort.getHost().port)
return super(PortReporter, self).privilegedStartService()
def stopService(self):
self.listeningPort.stopListening()
return super(PortReporter, self).stopService()
Run Code Online (Sandbox Code Playgroud)
然后,您可以在tac文件中使用它,如下所示:
from twisted.internet.protocol import Factory
from twisted.application.service import Application
application = Application("test")
def showPortNumber(n):
print("The port number is: %d" % (n,))
PortReporter(Factory(), showPortNumber).setServiceParent(application)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3599 次 |
| 最近记录: |