import json import urllib.request import time hostnameList = ['host1','host2','host3'] while True: for host in hostnameList: apidata = urllib.request.urlopen("http://localhost/jsonapi").read().decode('utf8') alive_status = json.loads(apidata)["hosts"][(host)]["alive"] if alive_status == True: print (str(host) + ' is alive. ') else: print (str(host) + ' is dead') time.sleep(10)
Условия для одного хоста:
if alive_status == True: if aliveMessageStatus == False: print ('alive') aliveMessageStatus = True deadMessageStatus = False if alive_status == False: if deadMessageStatus == False: print ('dead') deadMessageStatus = True aliveMessageStatus = False