简体   繁体   English

如何使用twisted来侦听多个udp端口?

[英]How do I listen to multiple udp ports using twisted?

I've written a server using Python and the Twisted library that communicates via UDP. 我用Python写了一个服务器,通过UDP进行通信的Twisted库。 This all works well. 这一切都运作良好。

What I would like to do is extend that server so that it can accept messages on multiple UDP ports simultaneously (I use the different ports to segregate the information returned, and it's less about the source of the request). 我想要做的是扩展该服务器,以便它可以同时接受多个UDP端口上的消息(我使用不同的端口来隔离返回的信息,而不是关于请求的来源)。 I've tried to do the simplest thing first as a test - I wrote code that looks like this: 我试图做最简单的事情作为测试 - 我写的代码看起来像这样:

reactor.listenUDP(port, handler)
reactor.listenUDP(port+1, handler)

(The first line is the one originally in my server; the second line is the 2nd port to listen to.) (第一行是我服务器中的第一行;第二行是要侦听的第二个端口。)

When I run that, I get the following: 当我运行时,我得到以下内容:

File "./rspServer.py", line 838, in mainLoop
  reactor.listenUDP(self.args.port+1, udpHandler)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/posixbase.py", line 347, in listenUDP
  p.startListening()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/udp.py", line 86, in startListening
  self._connectToProtocol()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/udp.py", line 106, in _connectToProtocol
  self.protocol.makeConnection(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/protocol.py", line 665, in makeConnection
  assert self.transport == None
AssertionError

I take from this that I can't add a second UDP listener, but that's exactly what I'd like to do (actually, to make a total of 18 listeners - it's a long story). 我从中可以看出,我无法添加第二个UDP侦听器,但这正是我想做的事情(实际上,总共有18个听众 - 这是一个很长的故事)。 Any thoughts on how to do this? 有关如何做到这一点的任何想法?

Each call to listenUDP() needs a unique handler instance. 每次调用listenUDP()都需要一个唯一的处理程序实例。 Other than that, your code looks fine. 除此之外,您的代码看起来很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM