-
Notifications
You must be signed in to change notification settings - Fork 0
/
threading_ping.py
executable file
·94 lines (74 loc) · 2.21 KB
/
threading_ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/env python
#
# Performance statistic
#
# ip_total 272
# --------------------------
# threads | usetime
# --------------------------
# 50 | 0.297966003418
# 20 | 0.317163944244
# 10 | 0.34256196022
# 5 | 0.422833919525
# 3 | 0.619158029556
# 2 | 1.09398913383
# 1 | 61.1227178574
# --------------------------
# ip_total 1333
# --------------------------
# threads | usetime
# --------------------------
# 50 | 1.30968809128
# 20 | 1.28201603889
# 10 | 1.31211805344
# 5 | 1.48950099945
# 3 | 2.03393101692
# 2 | 3.68216395378
# 1 | ???
# --------------------------
from random import randint
from time import sleep, time
from ping import quiet_ping
import Queue
import threading
def quietPing(ip_queue, nthread):
while True:
try:
if not ip_queue.empty():
ipaddr = ip_queue.get()
ping_result = quiet_ping(ipaddr, timeout=1)
print '[%s]Thread-%-2s %-18s %r' % (str(time()), nthread, ipaddr, ping_result)
else:
break
except:
print 'exception'
def iplistFilter():
'''
Filte the ip can not ping success
'''
with open('./iplist.py', 'r') as iplist1:
for ip in iplist1:
ping_result = quiet_ping(ip, timeout=1)
if ping_result[1]: # ping success
print ping_result
iplist_filted = open('./iplist_filted.py', 'a')
iplist_filted.write(ip)
iplist_filted.close()
def main():
ip_queue = Queue.Queue()
with open('./iplist_filted.py', 'r') as iplist:
for ip in iplist:
ip_queue.put(ip)
threads = []
for i in range(5):
th = threading.Thread(target=quietPing, args=(ip_queue, str(i)))
threads.append(th)
for t in threads:
t.start()
for t in threads:
t.join()
if __name__ == '__main__':
startime = time()
main()
print time() - startime
#iplistFilter()