简体   繁体   中英

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. 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). 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). Any thoughts on how to do this?

Each call to listenUDP() needs a unique handler instance. Other than that, your code looks fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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