Re: persistent TCP connection in python using socketserver

Discussion in 'Python' started by Jean-Paul Calderone, Jan 29, 2009.

  1. On Thu, 29 Jan 2009 08:38:43 -0800 (PST), 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
    Jean-Paul Calderone, Jan 29, 2009
    #1
    1. Advertising

  2. Jean-Paul Calderone

    Guest

    On Jan 30, 5:54 am, Jean-Paul Calderone <> wrote:
    > On Thu, 29 Jan 2009 08:38:43 -0800 (PST), 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 !
    , Jan 31, 2009
    #2
    1. Advertising

  3. Jean-Paul Calderone

    BlakeF Guest

    On Jan 29, 8:54 pm, Jean-Paul Calderone <> wrote:
    > On Thu, 29 Jan 2009 08:38:43 -0800 (PST), 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
    BlakeF, Feb 1, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathryn Bean
    Replies:
    1
    Views:
    672
    John Harrison
    Oct 28, 2004
  2. Tiger
    Replies:
    5
    Views:
    952
    Dave Thompson
    May 1, 2006
  3. Replies:
    0
    Views:
    342
  4. gk
    Replies:
    7
    Views:
    951
    Tom Anderson
    Oct 12, 2010
  5. mel
    Replies:
    9
    Views:
    162
    Thomas 'PointedEars' Lahn
    Apr 18, 2008
Loading...

Share This Page