python 多线程 ping

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()

转载于:https://www.cnblogs.com/Mrhuangrui/p/9260075.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值