AustinM AustinM - 3 months ago 6
Python Question

Python handling specific error codes?

Hey I'm wondering how to handle specific error codes. For example,

[Errno 111] Connection refused


I want to catch this specific error in the socket module and print something.

Answer

If you want to get the error code, this seems to do the trick;

import errno

try:
    socketConnection()
except socket.error, v:
    errorcode=v[0]
    if errorcode==errno.ECONNREFUSED:
        print "Connection Refused"

You can look up errno error codes.