我正在努力想出一种快速准确的方法,每隔五分钟就从我的数据库中ping出1000个网站。理想情况下,完成后需要不到1分钟。到目前为止,我只是对域运行ping
import os
import MySQLdb
db = MySQLdb.connect(host="localhost",
user="xxx",
passwd="xxx",
db="xxx")
cur = db.cursor()
cur.execute("SELECT * FROM monitors")
for row in cur.fetchall():
response = os.system("ping -c 1 " + row[4])
if response == 0:
print(row[4], 'is up!')
else:
print(row[4], 'is down!')
db.close()
我最终想要获得某种响应代码(200,503,404等)我看到你可以使用request
或urllib2
什么是实现类似内容的最佳选择此?
这是我的请求代码
import MySQLdb
import requests
db = MySQLdb.connect(host="localhost",
user="xxx",
passwd="xxx",
db="xxx")
cur = db.cursor()
cur.execute("SELECT * FROM monitors")
for row in cur.fetchall():
try:
r = requests.head("https://" + row[4])
print(r.status_code)
except requests.ConnectionError:
print("failed to connect")
db.close()