[FRPythoneers] Cannot Get SocketServer to Work

Dhruva B. Reddy bdhruva at gmx.net
Wed Sep 17 13:17:34 MDT 2003


OK, that worked--thanks Rob!

However, I tried replacing serve_forever() with handle_request() (which
causes the server to exit after handling a single request), and found
that it doesn't exit cleanly--the socket is in a TIME_WAIT state.

The same thing happens when I leave the serve_forever() call and kill the
server with a SIGINT.

Does anyone know how to prevent this?

Thanks,
-d

On Wed, 17 Sep 2003 at 12:29 -0600, Rob Riggs soliloquized thusly:
> 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.
> >
> >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`
> >
> >_______________________________________________
> >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
> > 
> >
> 
> 
> _______________________________________________
> 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

-- 
 "Those who would give up essential liberty to purchase a little temporary
 safety deserve neither liberty or safety. Nor, are they likely to end
 up with either."-- Benjamin Franklin




More information about the FRPythoneers mailing list