python中的HttpError都是IOError的子类吗

发布于 2024-08-10 21:21:33 字数 362 浏览 1 评论 0原文

在我们的代码中,我们捕获 IOError 并在重新引发之前将其记录下来。我收到“对等方重置连接”,但日志中没有任何内容。 python中的“连接由peer重置”是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')

In our code we catch IOError and log it before reraising. I am getting a "connection reset by peer", but nothing in the logs. Is "connection reset by peer" a subclass of IOError in python?

     .....
  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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-08-17 21:21:33

您粘贴的堆栈跟踪看起来像类 error 的一些异常,带有参数 (104, 'Connection Reset by Peer)。

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

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

来自 usawaretech 的评论:

你怎么知道它是一个套接字
错误?我的代码是这样的:
尝试:risky_code();除了 IO 错误:
logger.debug('...');增加;正如我一样
假设 HttpError 是一个子类
IOError,当我得到这个异常时,
我假设它被记录。那里
我的日志中没有任何内容

我猜它是一个 socket.error 因为我使用了标准库的 索引文档,并且因为我之前遇到过这个错误。

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

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

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

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

The stack trace you pasted looks like some Exception of class error with arguments (104, 'Connection reset by peer).

So it looks like it's not a HTTPError exception at all. It looks to me like it's actually a socket.error. This class is indeed a subclass of IOError since Python 2.6.

But I guess that's not your question, since you are asking about HttpError exceptions. Can you rephrase your question to clarify your assumptions and expectations?

Comment from 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

I guess it is a socket.error because I used the index of the standard library documentation, and because I encountered this error before.

What version of Python are you using? I guess it's Python 2.5 or earlier.

If your intent is to log and re-raise exceptions, it would be a better idea to use a bare except:

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

Also, you can find the module where the exception class was defined using exception.__module__.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文