0

I am looking for either of the above two Exceptions but cannot find them. In my existing errno I only have ECONNRESET.

I found a comment about python 3 (maybe time to upgrade?) and ConnectionRefused Error (here) but I cannot find the library that contains it.

Any help would be appreciated.

Thanks!

Community
  • 1
  • 1
zevij
  • 2,416
  • 1
  • 23
  • 32
  • possible duplicate of [Catch "socket.error: \[Errno 111\] Connection refused" exception](http://stackoverflow.com/questions/14425401/catch-socket-error-errno-111-connection-refused-exception) – Eric Renouf Sep 25 '15 at 00:15

1 Answers1

2

I see errno.ECONNREFUSED in the socket module.

ECONNREFUSED is not an exception though. socket throws an exception of type socket.error, and sets the errno field of the exception to tell you what kind it was. So you would do the following to check for ECONNREFUSED

import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
    sock.connect(('myhost', port))
except socket.error as sock_err:
    if(sock_err.errno == socket.errno.ECONNREFUSED):
        print "Connection was refused"

You can also see this in the answer by Martijn Pieters

Community
  • 1
  • 1
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Ok. I'm a bit confused: what is my import? socket? socket.error? what do I put in the except clause? Nothing seems to give me access to errno.ECONNREFUSED. Unless I do: except socket.error: if socket.errno.ECONNREFUSED – zevij Sep 24 '15 at 23:08
  • @goggelj I've updated the answer to hopefully help you – Eric Renouf Sep 25 '15 at 00:13