Hello, there was a problem when writing data to the database. Please tell me where to push "db.commit ()", thanks! Approximately I want to bring everything to this form.
import requests import time from subprocess import call, PIPE import MySQLdb Routers = [] Hosts = [] db = MySQLdb.connect(host="localhost", user="root", passwd="1234", db="sqlrst", charset='utf8') cursor = db.cursor() cursor.execute("SELECT Router FROM Routers") Routers = cursor.fetchall() cursor.execute("SELECT Host FROM Hosts") Hosts = cursor.fetchall() def HttpRequest(): for x in Hosts: for z in x: try: request = requests.get(z) request.raise_for_status() cursor.execute(" UPDATE Routers SET Response = 1 WHERE Router = %s ", (z,)) except requests.exceptions.RequestException: cursor.execute(" UPDATE Routers SET Response = 0 WHERE Router = %s ", (z,)) def Pinger(): for x in Routers: for z in x: p = call("ping -n 1 " + z, shell=True, stdout=PIPE, stderr=PIPE) if p != 0: print(x, "Unreachable") cursor.execute(" UPDATE Hosts SET Response = 0 WHERE Host = %s ", (z,)) else: cursor.execute(" UPDATE Hosts SET Response = 1 WHERE Host = %s ", (z,)) while True: HttpRequest() Pinger() time.sleep(3) 
UPDATEorMERGErequest. Well, certainly it is not necessary to check the connection and make a commit inside the loop at each iteration. - Dmitriy