[Twisted-Python] How to create a new service and listen on a new port?

Jp Calderone exarkun at divmod.com
Mon May 23 05:41:43 EDT 2005


On Mon, 23 May 2005 13:49:21 +0800, Eric Hsu <nkeric at gmail.com> wrote:
>Hi all,
>
>This is my first post here, and I'm not a native English speaker, so
>please bear with me :)
>I'm trying to implement something like this:
>
> * A UDP server listening on port1 for incoming request
> * A client sends the requset to port1
> * The server confirms the request and creates another (different) UDP
>service on a new port (port2)
> * The client then would be able to connect to port2 and do some other things...
>
>By reading this "Writing a New Plug-In for mktap"
> - http://twistedmatrix.com/projects/core/documentation/howto/plugin.html
>
>I've implemented a UDP server and could run it with "twisted -f".
>Now the server receives request on port1, and creates a new service at
>port2 for client to connect to it.
>
>I implemented this as the following:
>
>class FooServer(DatagramProtocol):
>    def datagramReceived(self, data, addr):
>        # do something to verify the request and send the client with
>a confirm msg
>        reactor.listenUDP(port2, AnotherService())
>        reactor.run()
>

  Since nothing is likely to call datagramReceived unless the reactor is running, and calling reactor.run() while the reactor is running is a terribly broken thing to do, the above code is likely mistaken in calling reactor.run().

  I bet shutdown behavioor will become much nicer if you remove the call to reactor.run() here, and in any other similar places.

  twistd will start and stop the reactor for you.

Jp




More information about the Twisted-Python mailing list