Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I listen to multiple udp ports using twisted?

Tags:

python

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?

like image 346
Cubs Fan Ron Avatar asked Jul 09 '12 14:07

Cubs Fan Ron


1 Answers

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

like image 130
Rakis Avatar answered Oct 14 '22 03:10

Rakis