捕获"socket.timeout读取操作超时".在python中 [英] catching "socket.timeout The read operation timed out" in python

查看:309
本文介绍了捕获"socket.timeout读取操作超时".在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用API,并收到以下超时错误:

I'm making calls to an API and am receiving the following timeout error:

socket.timeout The read operation timed out

可追溯到

File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/socket.py", line 576, in readinto
    return self._sock.recv_into(b)
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 937, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 799, in read
    return self._sslobj.read(len, buffer)
  File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 583, in read
    v = self._sslobj.read(len, buffer)
socket.timeout The read operation timed out

如何从追溯的顶部捕获此错误?

How can I catch this error from the top of the traceback?

推荐答案

这是一个重复的问题,但是正确的方法是使用

This is a duplicate question, however the correct approach is to catch socket.timeout by using

import socket
try:
    ...
except socket.timeout:
    ...

该错误指出显式引发的异常,您可以依赖该异常.即使调用方法是ssl lib,该错误似乎也与实际的套接字连接有关.

The error states the exception that was thrown explicitly and you can rely on that. Even though the calling method is the ssl lib, the error seems to be related to the actual socket connection.

这篇关于捕获"socket.timeout读取操作超时".在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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