[Twisted-Python] Dynamic listening to UDP ports?
Hi, Im trying to make a server that listens to one TCP socket for commands and then starts listening to more or less UDP sockets for data depending on the commands. The UDP data shall then be forwarded to one or more outgoing UDP ports. For example, here are some commands with explanations: addChannel 10000 should make the server listening to UDP port 10000 addChannel 10002 should make the server listening to UDP port 10002 addConnection 10000 10.0.0.2:5000 start forwarding the packet from UDP port 10000 to port 5000 on another machine addConnection 10000 10.0.0.3:5002 start forwarding the packet from UDP port 10000 to port 5002 on yet another machine delConnection 10000 10.0.0.2:5000 stop forwarding packets to the first guy delConnection 10000 10.0.0.3:5002 stop forwarding packets to the second guy delChannel 10000 stop listening to port 10000 Ive been making some rather advanced servers in twisted, but with fixed ports. How can I deal with this type of dynamic port bindings? For adding a port to listen to: reactor.listenUDP(10000,prot) is working, but how do I stop listening to a port an unbind it? Ive found something called unlistenUDP but it seems to be deprecated? Alternatively, I could start up a service , like u = internet.services.UDPServer(10000,UdpProtocol()) but how do I stop this particular service without stopping all others? Any suggestions are welcome. Regards, Torbjörn Einarsson1
To stop listening on UDP port: port = reactor.listenUDP(1243, p) port.stopListening()
On Tue, 26 Apr 2005 18:27:05 +0200, Torbjörn Einarsson <torbjorn@einarssons.se> wrote:
Hi,
Im trying to make a server that listens to one TCP socket for commands and then starts listening to more or less UDP sockets for data depending on the commands. The UDP data shall then be forwarded to one or more outgoing UDP ports.
For example, here are some commands with explanations:
addChannel 10000 should make the server listening to UDP port 10000
addChannel 10002 should make the server listening to UDP port 10002
addConnection 10000 10.0.0.2:5000 start forwarding the packet from UDP port 10000 to port 5000 on another machine
addConnection 10000 10.0.0.3:5002 start forwarding the packet from UDP port 10000 to port 5002 on yet another machine
delConnection 10000 10.0.0.2:5000 stop forwarding packets to the first guy
delConnection 10000 10.0.0.3:5002 stop forwarding packets to the second guy
delChannel 10000 stop listening to port 10000
Ive been making some rather advanced servers in twisted, but with fixed ports. How can I deal with this type of dynamic port bindings?
For adding a port to listen to: reactor.listenUDP(10000,prot) is working, but how do I stop listening to a port an unbind it? Ive found something called unlistenUDP but it seems to be deprecated?
listenUDP returns a udp.Port. Call stopListening() on that Port and it will shut down.
Alternatively, I could start up a service , like
u = internet.services.UDPServer(10000,UdpProtocol())
but how do I stop this particular service without stopping all others?
Just call stopService() on it. How you arrange to know which services should have this called on them and how you track your services depends on your specific application. Jp
Thanks a lot! A related question: how do I in the simplest way send UDP data to 10 different ports. In standard Python I would do sock = socket.socket(AF_INET, socket.SOCK_DGRAM) for port in portList: sock.sendto(data,(host,port)) sock.close() What is the corresponding thing in Twisted? (I've only found listenUDP and connected UDP). BR, Torbjörn -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Itamar Shtull-Trauring Sent: den 26 april 2005 18:52 To: Twisted general discussion Subject: Re: [Twisted-Python] Dynamic listening to UDP ports? To stop listening on UDP port: port = reactor.listenUDP(1243, p) port.stopListening() _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Tue, 26 Apr 2005 22:52:45 +0200, Torbjörn Einarsson <torbjorn@einarssons.se> wrote:
Thanks a lot!
A related question: how do I in the simplest way send UDP data to 10 different ports.
[snip]
Use the write() method. See the UDP howto: http://twistedmatrix.com/documents/current/howto/udp Jp
Well, the examples always use reactor.listenUDP(port,proto). Can I get rid of the listenUDP, since I don't want to listen to a port? I tried with just creating a protocol instance and using self.transport.write() inside it but then the transport was None. Sorry for being slow in grasping this, but the manual is very terse... /Torbjörn -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Jp Calderone Sent: den 27 april 2005 00:13 To: Twisted general discussion Subject: RE: [Twisted-Python] Dynamic listening to UDP ports? On Tue, 26 Apr 2005 22:52:45 +0200, Torbjörn Einarsson <torbjorn@einarssons.se> wrote:
Thanks a lot!
A related question: how do I in the simplest way send UDP data to 10 different ports.
[snip]
Use the write() method. See the UDP howto: http://twistedmatrix.com/documents/current/howto/udp Jp _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Hi, I'm just starting to write a simple client, whose protocol inherits from basic.LineReceiver: class MyClientProtocol(basic.LineReceiver): def connectionMade(self): stdout.write("\nconnectionMade\n") self.transport.write("test\r\n") # time.sleep(10) now if I un-comment the "time.sleep(10)", the server doesn't receive the "test\r\n" message until 10 seconds later, basically after the connectionMade() returns. Is there anyway to "flush" the buffer so the transport will send the "test\r\n" right away? Thanks. Z.
On Tue, 26 Apr 2005 16:30:36 -0700, sir shz <sir.shz@gmail.com> wrote:
Hi, I'm just starting to write a simple client, whose protocol inherits from basic.LineReceiver:
class MyClientProtocol(basic.LineReceiver): def connectionMade(self): stdout.write("\nconnectionMade\n") self.transport.write("test\r\n") # time.sleep(10)
now if I un-comment the "time.sleep(10)", the server doesn't receive the "test\r\n" message until 10 seconds later, basically after the connectionMade() returns. Is there anyway to "flush" the buffer so the transport will send the "test\r\n" right away?
Thanks.
Z.
Flushing a buffer is a potentially long-running operation. What if the buffer was full? What if the network is congested? What if the peer has gone away and will never, ever ACK another packet ever again? You should rely on protocol-level messages if you need to synchronize things. You should rely on protocol-level acknowledgements if you need to be certain of delivery. All you can rely on transport.write() to do is eventually send the bytes, if it is possible to do so. Jp
On Wed, 27 Apr 2005 01:01:29 +0200, Torbjörn Einarsson <torbjorn@einarssons.se> wrote:
Well, the examples always use reactor.listenUDP(port,proto). Can I get rid of the listenUDP, since I don't want to listen to a port?
I tried with just creating a protocol instance and using self.transport.write() inside it but then the transport was None.
Sorry for being slow in grasping this, but the manual is very terse...
You cannot get rid of the listenUDP(). All UDP traffic has two endpoint addresses associated with it. One on the receiving end, one on the sending end. The socket module example you provided earlier binds a local port, too, it just does so implicitly instead of explicitly. If you don't care what port you're bound to, use 0. Your socket library will pick an available one at random for you. Jp
Torbjörn Einarsson wrote:
Thanks a lot!
A related question: how do I in the simplest way send UDP data to 10 different ports.
In standard Python I would do sock = socket.socket(AF_INET, socket.SOCK_DGRAM) for port in portList: sock.sendto(data,(host,port)) sock.close()
What is the corresponding thing in Twisted? (I've only found listenUDP and connected UDP).
As I understand it you want to tie different servers together. The "Server Factory" base class for TCP connections is an abstraction layer which makes it possible to spawn different server protocols , see: http://twistedmatrix.com/documents/current/howto/tutorial/factory There is no UDP Factory base class that can be used to wrap different UDP protocol instances. I achieve this by writing my own application layer which ties everything together- see: http://twistedmatrix.com/pipermail/twisted-python/2005-March/009888.html Alternatively - I suspect one could also simply inherit from service.Application and code your "application layer" stuff in there. regards, Eugene Coetzee -- -- =============================================== Reedflute Software Solutions Telephone -> +27 18 293 3236 General information -> info@reedflute.com Project information -> projects@reedflute.com Web -> www.reedflute.com ===============================================
On Wed, 27 Apr 2005 10:14:10 +0200, Eugene Coetzee <projects@reedflute.com> wrote:
[snip]
Alternatively - I suspect one could also simply inherit from service.Application and code your "application layer" stuff in there.
You can't subclass t.a.service.Application (since it's a function :). Subclassing t.a.service.Service is often a good idea, though. Jp
Jp Calderone wrote:
You can't subclass t.a.service.Application (since it's a function :). Subclassing t.a.service.Service is often a good idea, though.
Jp
I stand corrected, although from an OO puritanical :) point of view "Application" should be the name of a class - not a method.
participants (5)
-
Eugene Coetzee
-
Itamar Shtull-Trauring
-
Jp Calderone
-
sir shz
-
Torbjörn Einarsson