Catch“socket.error: Connection refused”exception
我怎么能捕获socket.error: [Errno 111] Connection refusedexception?
try: senderSocket.send("Hello") except ?????: print "catch !"
通过捕获所有的 socket.errorexception,并且如果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