gpt4 book ai didi

python - 在 python 中捕获 "socket.timeout The read operation timed out"

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:47 24 4
gpt4 key购买 nike

我正在调用 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

我怎样才能从回溯的顶部捕获这个错误?

最佳答案

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

捕获 socket.timeout
import socket
try:
...
except socket.timeout:
...

该错误指出了明确抛出的异常,您可以依赖它。尽管调用方法是 ssl 库,但错误似乎与实际的套接字连接有关。

关于python - 在 python 中捕获 "socket.timeout The read operation timed out",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com