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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…