Python之判断活跃主机

1.call_ping.py

import subprocess
import threading
from Queue import Queue
from Queue import Empty
 
def call_ping(ip):
    if subprocess.call(['ping','-c','1',ip]):
        print("{0} is alive".format(ip))
    else:
        print("{0} is not alive".format(ip))
         
def ip_live(q):
    try:
        while True:
            ip = q.get_nowait()
            call_ping(ip)
    except Empty:
        pass
 
def main():
    q = Queue()
    with open('ip.txt') as f:
        for line in f:
            q.put(line)
             
        threads = []
        for i in range(10):
            thr = threading.Thread(target=ip_live,args=(q,))
            thr.start()
            threads.append(thr)
    for thre in threads:
        thre.join()
     
if __name__ == "__main__"
    main()


「 文章如果对你有帮助,请点个赞哦^^ 」 

0