I have noticed while programming a TCP server in Python that there are some errors that happen in different conditions when one end or the other stops unexpectedly.
For example, sometimes I got “Pipe broken” (errno.EPIPE) and sometimes “connection aborted” (errno.CONNABORTED or errno.WSAECONNABORTED). There’s also the thing that across OSs the codes are not the same, but I guess Python’s errno module handles that.
I searched a lot for a list of the meanings of the error codes of socket connection, without finding what I was looking for.
What I’ve got until now is something like:
try:
# write or read operation
except socket.error as e:
if e.errno in (errno.EPIPE, errno.ECONNABORTED, errno.WSAECONNABORTED):
print 'Connection lost with server...'
Until now, everything was working smoothly, and even before adding the last one, I had a problem on Windows, and added it, so I’m afraid there might be some cases I did not handle. Also, sometimes, it just didn’t throw an error and kept reading empty lines (with recv), and Bad file descriptor, etc.
Does the SocketServer class provide such a thing? Or TCP connections in general?
When you try to read from a closed socket in python, no Exception is usually raised. You should just read until
recvreturns the emty string.Writing to a closed socket of course raises an Execption (
socket.error) which wraps the error number that the OS raises.But you shouldn’t concern yourself too much with error codes. Python isn’t C, or as the tutorial puts it when talking about nonblocking sockets: