Catch“socket.error: Connection refused”exception
我怎么能捕获socket.error: [Errno 111] Connection refused
exception?
try: senderSocket.send("Hello") except ?????: print "catch !"
通过捕获所有的 socket.error
exception,并且如果errno
属性不等于111,则重新提高它。或者,更好的是使用errno.ECONNREFUSED
常量:
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