我正在调用API并收到以下超时错误:
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
如何从回溯顶部捕获此错误?
答案 0 :(得分:1)
这是一个重复的问题,但正确的方法是使用
捕获socket.timeout
import socket
try:
...
except socket.timeout:
...
错误说明了显式抛出的异常,您可以依赖它。即使调用方法是ssl lib,错误似乎与实际的套接字连接有关。
答案 1 :(得分:0)
谷歌搜索导致我https://community.home-assistant.io/t/difficulty-catching-an-exception/12955/6
似乎SSL在超时时抛出ssl.SSLError
。你可以尝试类似的东西:
import ssl
def getresponse():
...
try:
response.begin
except ssl.SSLError as err:
handle_error(err)