首页 > 编程语言 > 详细

python 多线程 ping

时间:2018-07-03 18:54:37      阅读:181      评论:0      收藏:0      [点我收藏+]

python 多线程 ping

多线程操作可按如下例子实现

#!/usr/bin/env python
#encoding: utf8

import subprocess
from threading import Thread   
from Queue import Queue  

def ping(i,queue):  
    while True:  
        ip=queue.get()  
        #print 'Thread %s pinging %s' %(i,ip)  
        ret=subprocess.call('ping -c 1 %s' % ip,shell=True,stdout=subprocess.PIPE)  
        if ret==0:  
            print '%s is alive!' %ip  
        elif ret==1:  
            print '%s is down...'%ip  
        queue.task_done()  
  
q=Queue()
ips=['127.0.0.1','116.56.148.187','47.97.184.87']
#command = "awk '{print $1}' iplist"
#p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE)
#ips = p.stdout.read().split('\n')
#while '' in ips:
#    ips.remove('')
    
for i in range(5):  
    t=Thread(target=ping,args=(i,q))  
    t.setDaemon(True)  
    t.start()  

for ip in ips:  
    q.put(ip)  
q.join()

python 多线程 ping

原文:https://www.cnblogs.com/Mrhuangrui/p/9260075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!