How could I catch socket.error: [Errno 111] Connection refused
exception ?
try: senderSocket.send("Hello") except ?????: print "catch !"
For example, the classic Connection refused errno 111 means that the initial SYN packet to the host you connect() to was responded with an RST packet instead of the normal SYN+ACK — which usually happens when there's no program listening to the given port on the remote computer you connect() to.
10061 is a Connection Refused error sent to you by the server. You could not make a connection because the target machine actively refused it. The most common cause is a misconfigured server, full server, or incorrect Port specified by the client.
By catching all socket.error
exceptions, and re-raising it if the errno
attribute is not equal to 111. Or, better yet, use the errno.ECONNREFUSED
constant instead:
import errno from socket import error as socket_error try: senderSocket.send('Hello') except socket_error as serr: if serr.errno != errno.ECONNREFUSED: # Not the error we are looking for, re-raise raise serr # connection refused # handle here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With