python - python中的HttpError都是IOError的子类吗

标签 python ioerror

在我们的代码中,我们捕获 IOError 并在重新引发之前将其记录下来。我收到“对等方重置连接”,但日志中没有任何内容。 python 中的“连接由对等方重置”是 IOError 的子类吗?

     .....
  File "/usr/lib/python2.5/httplib.py", line 1047, in readline
    s = self._read()
  File "/usr/lib/python2.5/httplib.py", line 1003, in _read
    buf = self._ssl.read(self._bufsize)
error: (104, 'Connection reset by peer')

最佳答案

您粘贴的堆栈跟踪看起来像带有参数(104,'连接由对等方重置)的类error的异常。

所以看起来它根本不是 HTTPError 异常。在我看来,它实际上是一个 socket.error 。从 Python 2.6 开始,这个类确实是 IOError 的子类。

但我想这不是你的问题,因为你问的是 HttpError 异常。您能否重新表述您的问题以澄清您的假设和期望?

来自 usawaretech 的评论:

How are you finding out it is a socket error? MY code is something like: try:risky_code(); except IOError: logger.debug('...'); raise; As I am assuming that HttpError is a subclass of IOError, when I get that exception, I am assuming that it be logged. There is nothing in my logs

我猜这是一个socket.error,因为我使用了index of the standard library documentation ,而且因为我以前遇到过这个错误。

您使用的是哪个版本的 Python?我猜它是 Python 2.5 或更早版本。

如果您的目的是记录并重新引发异常,那么最好使用裸异常:

try:
    risky_code()
except:
    logger.debug(...)
    raise

此外,您还可以使用 exception.__module__ 找到定义异常类的模块。

关于python - python中的HttpError都是IOError的子类吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1696195/

相关文章:

python pysftp [Errno 13] 权限被拒绝 :

python - 帮助解决读取文件时出现 IOError 的问题

创建长文件时出现 Python IOError 异常

python - 对某些数字之间的索引行求和 - Pandas Python

python - 为什么它说 Global Name not Defined 而不是? Python

python - 如何标准缩放 3D 矩阵?

python - 快速从txt读写时,如何防止IO权限被拒绝

java - FileUtils.copyFile 对大文件失败

python - 如何取消连接 pandas 数据框?

python - 使用 FTPwalker 连接到 FTP 资源失败,错误代码为 "Name or service not known"