有一个 ping 服务器的代码,以及那些响应 ping 的代码 - 通过 API 发出请求。写了一个循环:
def NoStatsToday():
for x in Online:
url = 'http://' + x + ':8200/api/v1/detection_storage/get_detections_stat?datetime_start=' + today + 'T00:00:00%2B05:00&datetime_end=' + today + 'T23:59:59%2B05:00'
response = requests.get(url, timeout=5)
forparse = response.json()
all_detections = (forparse["statistic"]['all_detections'])
try:
response = requests.get(url, timeout=5)
except ConnectionError:
print(x + ' Нет подключение к порту 8200')
else:
if all_detections != 0:
print(x + ' Есть статистика')
else:
print(x + ' Нет статистики')
nostatsfile.write(x + pingfile.write(url + now))
如果8200端口不可用,理论上应该写
except ConnectionError:
print(x + ' Нет подключение к порту 8200')
根据 CrazyElf 的建议,我进行了更正,如果我理解正确,它的意思是这样的:
def NoStatsToday():
for x in Online:
url = 'http://' + x + ':8200/api/v1/detection_storage/get_detections_stat?datetime_start=' + today + 'T00:00:00%2B05:00&datetime_end=' + today + 'T23:59:59%2B05:00'
try:
response = requests.get(url, timeout=5)
forparse = response.json()
all_detections = (forparse["statistic"]['all_detections'])
if all_detections != 0:
print(x + ' Есть статистика')
else:
print(x + ' Нет статистики')
nostatsfile.write(x + pingfile.write(url + now))
except ConnectionError:
print(x + ' Нет подключения к порту 8200')
现在好像没有crash了,请问知道的人检查一下是否还有错误导致数据显示不正确或者代码crash?
因此,需要输入所有内容
try才能使其except正常工作:什么
else意思,我不太明白。克for?无论如何,你需要all_detections在循环之前做一些初始化,否则,如果没有一个成功的动作,你的代码就会掉线if all_detections != 0: