[FRPythoneers] Cannot Get SocketServer to Work

Rob Riggs rob at pangalactic.org
Wed Sep 17 12:29:33 MDT 2003

Try self.rfile.readline() and send a newline terminated string in the 
client.  I don't understand why read() does not return until the remote 
end closes, but you aren't the first to run into this problem.

Dhruva B. Reddy wrote:

>I'm trying to write a simple socket server and client, using SocketServer.
>I use TCPServer, and my request handler subclasses StreamRequestHandler.
>The server hangs when it tries to read the request.  The call to rfile.read()
>does not return until the client is killed.
>Can someone tell me what I might be doing wrong?
>The code for the client and server follows.
>### Server ###
>import SocketServer, sys
>from MyRequestHandler import MyRequestHandler
>SocketServer.TCPServer(('localhost', 8081), MyRequestHandler).serve_forever()
>from SocketServer import StreamRequestHandler
>import inspect
>class MyRequestHandler(StreamRequestHandler):
>    def __init__(self, request, client_address, server):
>        StreamRequestHandler.__init__(self, request, client_address, server)
>    def handle(self):
>        print "About to read request..."
>	# The following does not return until the client dies...
>        print self.rfile.read()
>        # The following executes without error after the client dies...
>        print "About to write response..."
>        self.wfile.write('booyah!')
>        print "Wrote response."
>### Client ###
>import socket
>HOST = 'localhost'
>PORT = 8081
>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>s.connect((HOST, PORT))
>stuff = 'Hello, world'
>print 'Length of stuff = %d' % len(stuff)
>print s.send(stuff)
>data = s.recv(1024)
>print 'Received', `data`
>This message sent by the FRPythoneers mailing list.
>Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
>URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers

More information about the FRPythoneers mailing list