persistent TCP connection in python using socketserver
BlakeF
Platcool at gmail.com
Sun Feb 1 08:47:49 EST 2009
On Jan 29, 8:54 pm, Jean-Paul Calderone <exar... at divmod.com> wrote:
> On Thu, 29 Jan 2009 08:38:43 -0800 (PST), markobrie... at gmail.com wrote:
> >G'day
>
> >I'm currently usingsocketserverto 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
> >socketserverterminates the connection. Which is bad, since i need a
> >persistentconnection 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. SocketServerimplements 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 theSocketServermodule 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
Hi,
Jean-Paul has something like this in mind (I think):
class Foobar(BaseRequestHandler):
def handle(self):
self.data = None
while self.data != 'QUIT':
self.data = self.request.recv(1024).strip().upper()
if self.data == '':
sleep(1)
else:
self.request.send(self.data)
This will keep a persistent connection, only closing on 'quit' being
received. I'm sure it's not the best way, but it certainly works.
Cheers,
-Blake
More information about the Python-list
mailing list