1 import threading
2 import subprocess
3 import pymysql
4 # threading.Lock()
5
6
7 class Link(object):
8
9 ip = list()
10 def __init__(self):
11
12 self.connection = pymysql.connect(host='localhost', port=3306, user='**', passwd='**', db='**',
13 charset='utf8', autocommit=True)
14
15 self.cursor = self.connection.cursor()
16
17 sql = "select ip from ip"
18 self.all = self.cursor.execute(sql)
19 for i in self.cursor.fetchall():
20 Link.ip.append(i[0])
21 self.lock = threading.Lock()
22
23
24 def ping_ip(self,ip):
25
26 res = subprocess.call('ping -n 4 -w 5 %s' % ip, stdout=subprocess.PIPE)
27 print(res,">>>",ip,"<<<")
28 if res == 0:
29
30 sql = """UPDATE ip SET action = '在线' where ip like '%{}';""" .format(ip)
31 self.lock.acquire()
32 self.cursor.execute(sql)
33 self.lock.release()
34 print('通',ip)
35 else:
36 print("不通",ip)
37
38
39 def run(self):
40 for i in Link.ip: # global take
41 take = threading.Thread(target=self.ping_ip,args=(i,))
42 take.start()
43
44 take.join()
45
46
47 def main():
48 run = Link()
49 run.run()
50
51
52
53
54 if __name__ == '__main__':
55 main()