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.

Sketched in 2 minutes

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) 
  • one
    H'm ... and what after after checking the last condition again to check availability of a connection to a DB? it seems like you can’t see the break of communication ... - Akina
  • This is necessary to check the new records in the database. I just wanted to know how to correctly write the data in the database, thanks. - Mikhail Smirnov
  • I understand correctly that you want to take data in one table and put it in another? In general, relational databases like when this is done by a single UPDATE or MERGE request. Well, certainly it is not necessary to check the connection and make a commit inside the loop at each iteration. - Dmitriy
  • There is a program that checks the connection with routers by ping request. The response received must be imported into the same table from which the router's address is taken. The task is simple, but I can't solve it :( - Mikhail Smirnov

0