Reputation: 7078
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?
Upvotes: 3
Views: 3599
Reputation: 48335
The Python socket module is a mostly thin wrapper around the BSD socket API. Frequently, you can find documentation for the possible error codes (errno values) by looking at the manual pages for the C BSD socket API. For example, man 2 recv
:
ERRORS
These are some standard errors generated by the socket layer. Additional errors
may be generated and returned from the underlying protocol modules; see their
manual pages.
EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and the receive operation would
block, or a receive timeout had been set and the timeout expired before
data was received. POSIX.1-2001 allows either error to be returned for
this case, and does not require these constants to have the same value,
so a portable application should check for both possibilities.
EBADF The argument sockfd is an invalid descriptor.
ECONNREFUSED
A remote host refused to allow the network connection (typically because
it is not running the requested service).
EFAULT The receive buffer pointer(s) point outside the process's address space.
EINTR The receive was interrupted by delivery of a signal before any data were
available; see signal(7).
EINVAL Invalid argument passed.
ENOMEM Could not allocate memory for recvmsg().
ENOTCONN
The socket is associated with a connection-oriented protocol and has not
been connected (see connect(2) and accept(2)).
ENOTSOCK
The argument sockfd does not refer to a socket.
The manually pages are often incomplete themselves, but they cover more cases than any of the Python documentation.
Upvotes: 1
Reputation: 69082
When you try to read from a closed socket in python, no Exception is usually raised. You should just read until recv
returns 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:
You can check return code and error codes and generally drive yourself crazy. If you don’t believe me, try it sometime. Your app will grow large, buggy and suck CPU. So let’s skip the brain-dead solutions and do it right.
...
Upvotes: 1