捕获“socket.error: [Errno 111] 连接被拒绝";例外 [英] Catch "socket.error: [Errno 111] Connection refused" exception

查看:56
本文介绍了捕获“socket.error: [Errno 111] 连接被拒绝";例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能捕获socket.error: [Errno 111] Connection denied 异常?

How could I catch socket.error: [Errno 111] Connection refused exception ?

try:
    senderSocket.send("Hello")
except ?????:
    print "catch !"     

推荐答案

通过捕获所有 socket.error 异常,并在 errno 时重新引发它 属性不等于 111.或者,更好的是,使用 errno.ECONNREFUSED 改为常量:

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

这篇关于捕获“socket.error: [Errno 111] 连接被拒绝";例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆