Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

persistent TCP connection in python using socketserver

298 views
Skip to first unread message

markob...@gmail.com

unread,
Jan 29, 2009, 11:38:43 AM1/29/09
to
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.

I need to create TCP connection that persists until I explicitly tell
it to terminate, or the connection is terminated on the other end. A
quick look through the socketserver code makes this seem impossible
since it appears to call close after the handle() method.

Is there an even lower level library to be using, or is there some
option I haven't found yet in socketserver.

Cheers

Jean-Paul Calderone

unread,
Jan 29, 2009, 1:54:26 PM1/29/09
to pytho...@python.org
On Thu, 29 Jan 2009 08:38:43 -0800 (PST), markob...@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

markob...@gmail.com

unread,
Jan 31, 2009, 12:31:52 PM1/31/09
to
On Jan 30, 5:54 am, Jean-Paul Calderone <exar...@divmod.com> wrote:

> On Thu, 29 Jan 2009 08:38:43 -0800 (PST), markobrie...@gmail.com wrote:
> >G'day
>
> >I'm currentlyusingsocketserverto build a simple XMLSocket (an XML

> >based protocol used for communication between flash and the outside
> >world) server. I've got flash establishing aconnection, sending a
> >request and mypythonserver responding. However at this point
> >socketserverterminates theconnection. Which is bad, since i need a
> >persistentconnectionso i can push data from the server to the client

> >without the overhead of polling.
>
> If you don't want theconnectionto close, then don't let the request

> complete.  SocketServerimplements logic for single request/response
> perconnection.  You can change this by making your requests take a
> really long time (until you're done with theconnection) or you can
> override the behavior which closes theconnectionafter 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

Cheers mate I had a look into twisted but was put off by the FAQ
stating 1.0+ modules may or may not be stable, and only the 'core' is.
I don't wanna be messing around with a potentially buggy server, so im
gonna roll my own using the sockets module.


Thanks for your help !

Stephen Hansen

unread,
Jan 31, 2009, 6:30:48 PM1/31/09
to markob...@gmail.com, pytho...@python.org


Cheers mate I had a look into twisted but was put off by the FAQ
stating 1.0+ modules may or may not be stable, and only the 'core' is.
I don't wanna be messing around with a potentially buggy server, so im
gonna roll my own using the sockets module.


You didn't read that FAQ right: its addressing API stability. Meaning,
"Are you going to keep this API next release or is it gonna change?"
With your description of what you're doing, I'd be very surprised if you
needed to wander near an unstable/experimental module with a still
shifting API.

Twisted is VERY stable.

--S
signature.asc

BlakeF

unread,
Feb 1, 2009, 8:47:49 AM2/1/09
to
On Jan 29, 8:54 pm, Jean-Paul Calderone <exar...@divmod.com> wrote:

> On Thu, 29 Jan 2009 08:38:43 -0800 (PST), markobrie...@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

Jean-Paul Calderone

unread,
Feb 3, 2009, 11:23:43 AM2/3/09
to pytho...@python.org
On Sat, 31 Jan 2009 09:31:52 -0800 (PST), markob...@gmail.com wrote:
> [snip]

>
>Cheers mate I had a look into twisted but was put off by the FAQ
>stating 1.0+ modules may or may not be stable, and only the 'core' is.
>I don't wanna be messing around with a potentially buggy server, so im
>gonna roll my own using the sockets module.
>

For what it's worth, that FAQ entry was grossly out of date. I just
deleted it and replaced it with an entry which says the exact opposite.

Jean-Paul

0 new messages