捕获“ socket.error:[Errno 111]连接被拒绝”异常

我怎样才能捕捉到socket.error: [Errno 111] Connection refused异常?

try:

senderSocket.send("Hello")

except ?????:

print "catch !"

回答:

通过捕获 所有

socket.error异常,并在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

以上是 捕获“ socket.error:[Errno 111]连接被拒绝”异常 的全部内容, 来源链接: utcz.com/qa/433936.html

回到顶部