[FRPythoneers] Cannot Get SocketServer to Work

Dhruva B. Reddy bdhruva at gmx.net
Wed Sep 17 11:27:10 MDT 2003


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.

Thanks,
Dhruva

### Server ###
#!/usr/bin/python
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 ###
#!/usr/bin/python
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)
s.close()
print 'Received', `data`




More information about the FRPythoneers mailing list