我正在连接到API流.此连接始终处于活动状态,并且我从此API接收数据.我的问题是,有时连接关闭,我需要再次连接,但我的代码没有为这个关闭的连接打开任何异常.就好像连接仍然处于活动状态,但没有接收到任何数据.我如何知道连接何时断开?
我的代码是:
try:
response = requests.get(url, stream=True)
response.raise_for_status()
for line in response.iter_lines():
if line:
print(line)
else:
print("No data returned.")
except requests.exceptions.RequestException as e:
print("Error connection:", e)