One more socket programming question

Discussion in 'Python' started by John Salerno, Jun 18, 2008.

  1. John Salerno

    John Salerno Guest

    I'm now experimenting with the SocketServer class. Originally I
    subclassed the StreamRequestHandler to make my own custom handler, but a
    result of this seems to be that the client socket closes after it has
    been used, instead of staying open.

    Just as a test, I decided to use BaseRequestHandler instead, because I
    know its methods aren't implemented. So this is what I have:

    -----
    import SocketServer

    host = ''
    port = 51234
    address = (host, port)
    buffer_size = 1024

    class MyRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
    print '...connected from:', self.client_address
    data = self.request.recv(buffer_size)
    self.request.send('%s %s' % ('You typed:', data))

    socket_server = SocketServer.TCPServer(address, MyRequestHandler)
    print 'waiting for connection...'
    socket_server.serve_forever()
    ------

    ------
    from socket import *

    host = 'localhost'
    port = 51234
    address = (host, port)
    buffer_size = 1024

    client_socket = socket(AF_INET, SOCK_STREAM)
    client_socket.connect(address)

    while True:
    data = raw_input('> ')
    if not data:
    break
    client_socket.send(data)
    data = client_socket.recv(buffer_size)
    print data

    client_socket.close()
    ------

    But this only seems to work one time, and then subsequent attempts
    return nothing, and then the client program seems to crash (or just
    close on its own).

    What's happening here?
    John Salerno, Jun 18, 2008
    #1
    1. Advertising

  2. John Salerno

    Tim Roberts Guest

    John Salerno <> wrote:
    >
    >I'm now experimenting with the SocketServer class. Originally I
    >subclassed the StreamRequestHandler to make my own custom handler, but a
    >result of this seems to be that the client socket closes after it has
    >been used, instead of staying open.


    Right. "handle" is not called for one REQUEST at a time, it's called for
    one CONNECTION at a time. If you need a connection to be persistent, then
    your handle() function needs to sit in a loop making recv calls until you
    detect that the conversation is complete.
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jun 18, 2008
    #2
    1. Advertising

  3. John Salerno

    John Salerno Guest

    "Tim Roberts" <> wrote in message
    news:...
    > John Salerno <> wrote:
    >>
    >>I'm now experimenting with the SocketServer class. Originally I
    >>subclassed the StreamRequestHandler to make my own custom handler, but a
    >>result of this seems to be that the client socket closes after it has
    >>been used, instead of staying open.

    >
    > Right. "handle" is not called for one REQUEST at a time, it's called for
    > one CONNECTION at a time. If you need a connection to be persistent, then
    > your handle() function needs to sit in a loop making recv calls until you
    > detect that the conversation is complete.


    Ah, so I need to rewrite my handle method. I was thinking that the
    specialized setup() and/or finish() calls from StreamRequestHandler were the
    reason that the connection was closed after one use (which is why I tried
    BaseRequestHandler instead).

    Thanks.
    John Salerno, Jun 18, 2008
    #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. Laszlo Nagy
    Replies:
    1
    Views:
    4,757
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    942
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
  4. Steven D'Aprano
    Replies:
    0
    Views:
    75
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    68
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page