SocketServer crash on my machine
Gordon McMillan
gmcm at hypernet.com
Wed Jun 27 20:42:06 EDT 2001
Syver Enstad wrote:
>I was checking out the the SocketServer example in Mark Lutz rather
>excellent book Programming Python 2 edition,
>when
[snip]
> data = self.request.recv(1024)
> File "<string>", line 1, in recv
>AttributeError: 'int' object has no attribute 'recv'
The socket has already been closed.
[more snippage]
>class MyClientHandler(SocketServer.BaseRequestHandler):
> def handle(self):
> print self.client_address, now()
> print self.request
> time.sleep(5)
> while 1:
> data = self.request.recv(1024)
> if not data:
> break
> self.request.send('Echo=>%s at %s' % (data, now()))
> self.request.close()
The code is wrong. It assumes that it is in charge of closing the
socket (or that a closed socket will yield no data - which is just
one way that you may find out a socket is closed).
Different platforms show different behavior here (and behavior may
be influenced by subtle timing issues, and whether or not the socket
is blocking or non-blocking).
I would guess that you got one "Echo=> ....", then the client receiving
the echo shut down the socket, and you died on the 2nd recv. Always
put send and recv within
try:
...
except (AttributeError, socket.error):
...
- Gordon
More information about the Python-list
mailing list