gpt4 book ai didi

Python 和 MySQL 更新

转载 作者:行者123 更新时间:2023-11-28 23:29:24 25 4
gpt4 key购买 nike

ping 工具一切正常,但 MySQL 更新失败。

它应该拉取当前正在执行的 ip 地址并在 MYSQL 中更新它。

import MySQLdb
db = MySQLdb.connect(host="10.1.1.151", # your host, usually localhost
user="root", # your username
passwd="**************", # your password
db="main_system") # name of the data base
cur = db.cursor()

from threading import Thread
import subprocess
from Queue import Queue

num_threads = 10
queue = Queue()

ips = ["10.1.1.151", "10.1.1.152"]

#wraps system ping command
def pinger(i, q):
"""Pings subnet"""
while True:
ip = q.get()
ret = subprocess.call("ping -i .1 -c 1 -W 50 %s" % ip,
shell=True,
stdout=open('/dev/null', 'w'),
stderr=subprocess.STDOUT)
if ret == 0:
cur.execute("UPDATE application_status SET status = 1 WHERE ip_address = %s")
print "%s: is alive" % ip
else:
cur.execute("UPDATE application_status SET status = 0 WHERE ip_address = %s")
print "%s: did not respond" % ip
q.task_done()
#Spawn thread pool
for i in range(num_threads):

worker = Thread(target=pinger, args=(i, queue))
worker.setDaemon(True)
worker.start()
#Place work in queue
for ip in ips:
queue.put(ip)
#Wait until worker threads are done to exit
queue.join()

最佳答案

您缺少数据 (ip) 到游标的绑定(bind):

if ret == 0:
cur.execute("UPDATE application_status SET status = 1 WHERE ip_address = %s", (ip,))
print "%s: is alive" % ip
else:
cur.execute("UPDATE application_status SET status = 0 WHERE ip_address = %s", (ip,))
print "%s: did not respond" % ip

关于Python 和 MySQL 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com