persistent TCP connection in python using socketserver

Jean-Paul Calderone exarkun at divmod.com
Thu Jan 29 19:54:26 CET 2009


On Thu, 29 Jan 2009 08:38:43 -0800 (PST), markobrien85 at gmail.com wrote:
>G'day
>
>I'm currently using socketserver to build a simple XMLSocket (an XML
>based protocol used for communication between flash and the outside
>world) server. I've got flash establishing a connection, sending a
>request and my python server responding. However at this point
>socketserver terminates the connection. Which is bad, since i need a
>persistent connection so i can push data from the server to the client
>without the overhead of polling.

If you don't want the connection to close, then don't let the request
complete.  SocketServer implements logic for single request/response
per connection.  You can change this by making your requests take a
really long time (until you're done with the connection) or you can
override the behavior which closes the connection after a response.

Or you could use the socket module, on which the SocketServer module is
based.  Or you could use Twisted, another higher-level package built on
the socket module (mostly).  Actually, I recommend Twisted, since it will
mostly isolate you from boring low-level details and let you implement
whatever high-level behavior you're after (I know that a bunch of people
have used it to communicate with Flash, for example).

Jean-Paul



More information about the Python-list mailing list