AustinM AustinM - 1 year ago 63
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 Source

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

import errno

except socket.error, v:
    if errorcode==errno.ECONNREFUSED:
        print "Connection Refused"

You can look up errno error codes.