Understanding Python Error Codes and Using Names with Exact Error

I understand the basic syntax try: except: finally:for handling pythons errors. I do not understand how to find the correct error names to make readable code.

For instance:

try:
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect((HOST, PORT))
     s.settimeout(60)               
     char = s.recv(1)

except socket.timeout:
    pass

therefore, if the socket causes a timeout, the error is caught. How about if I look for a connection refused. I know the error number is 10061. Where in the documentation I am looking to find the value of a full name, such as a timeout. Would a similar place look for other python modules? I know this is a newbie question, but I have been processing my code for some time, not knowing where to look for error descriptions and names.

EDIT:

Thanks for all your answers.

will be

except socket.error, exception:
    if exception.errno == ETIMEDOUT:
         pass

achieve the same result as

except socket.timeout:
    pass
+5
3

, , , , , if errno :

try:
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect((HOST, PORT))
     s.settimeout(60)               
     char = s.recv(1)
except socket.error, exception:
    if exception.errno == errno.ECONNREFUSED:
        # this is a connection refused
    # or in a more pythonic way to handle many errors:
    {
       errno.ECONNREFUSED : manage_connection_refused,
       errno.EHOSTDOWN : manage_host_down,
       #all the errors you want to catch
    }.get(exception.errno, default_behaviour)()
except socket.timeout:
    pass

:

def manage_connection_refused():
   print "Connection refused"

def manage_host_down():
   print "Host down"

def default_behaviour():
   print "error"
+5

errno, errno. 10061 WinSock.

+2

According to socket , socket.error values ​​are defined in errno .

0
source

All Articles