如何捕获此 Python 异常:错误:[Errno 10054] 远程主机强制关闭现有连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18832643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:59:22  来源:igfitidea点击:

How to catch this Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

pythonsocketsexceptionpython-2.7

提问by SilentSteel

I am trying to catch this particular exception (and only this exception) in Python 2.7, but I can't seem to find documentation on the exception class. Is there one?

我试图在 Python 2.7 中捕获这个特定的异常(并且只有这个异常),但我似乎无法找到有关异常类的文档。有吗?

[Errno 10054] An existing connection was forcibly closed by the remote host

My code so far:

到目前为止我的代码:

try:
  # Deleting filename
  self.ftp.delete(filename)
  return True
except (error_reply, error_perm, error_temp):
  return False
except # ?? What goes here for Errno 10054 ??
  reconnect()
  retry_action()

采纳答案by user2489743

The error type is socket.error, the documentation is here. Try modiffying your code like this:

错误类型是 socket.error,文档在这里。尝试像这样修改你的代码:

import socket
import errno  

try:
    Deleting filename
    self.ftp.delete(filename)
    return True
except (error_reply, error_perm, error_temp):
    return False
except socket.error as error:
    if error.errno == errno.WSAECONNRESET:
        reconnect()
        retry_action()
    else:
        raise

回答by giovanni

You may try doing something like :

您可以尝试执行以下操作:

try:
    # Deleting filename
    self.ftp.delete(filename)
    return True
except (error_reply, error_perm, error_temp):
    return False
except Exception, e:
    print type(e)  # Should give you the exception type
    reconnect()
    retry_action()

回答by tdelaney

When you want to filter exceptions, the first step is to figure out the exception type and add it to an except clause. That's normally easy because python will print it out as part of a traceback. You don't mention the type, but it looks like socket.gaierror to me, so I'm going with that.

当您要过滤异常时,第一步是找出异常类型并将其添加到一个 except 子句中。这通常很容易,因为 python 会将它作为回溯的一部分打印出来。你没有提到类型,但对我来说它看起来像 socket.gaierror,所以我会这样做。

The next step is to figure out what is interesting inside of the exception. In this case, `help(socket.gaierror)' does the trick: there is a field called errno that we can use to figure out which errors we want to filter.

下一步是找出异常内部的有趣之处。在这种情况下,“help(socket.gaierror)”可以解决问题:有一个名为 errno 的字段,我们可以使用它来确定要过滤哪些错误。

Now, rearrange your code so that the exception is caught inside a retry loop.

现在,重新排列您的代码,以便在重试循环中捕获异常。

import socket

retry_count = 5  # this is configured somewhere

for retries in range(retry_count):
    try:
        # Deleting filename
        self.ftp.delete(filename)
        return True
    except (error_reply, error_perm, error_temp):
        return False
    except socket.gaierror, e:
        if e.errno != 10054:
            return False
        reconnect()
return False