国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python基于多線程實現ping掃描功能示例

2020-02-15 22:29:38
字體:
來源:轉載
供稿:網友

本文實例講述了Python基于多線程實現ping掃描功能。分享給大家供大家參考,具體如下:

# -*- coding:utf-8 -*-#! python2import subprocessfrom Queue import Queueimport threadingclass Pinger(object):  def __init__(self, ip_list, thread_num=2):    self._ip_list = ip_list    self._thread_num = thread_num    self._queue = Queue(len(ip_list))  def ping(self, thread_id):    while True:      if self._queue.empty():        break      addr = self._queue.get()      print 'Thread %s: Ping %s' % (thread_id, addr)      ret = subprocess.call('ping -c 1 %s' % (addr),                 shell=True,                 stdout=open("/dev/null", 'w'),                 stderr=subprocess.STDOUT)      if ret == 0:        print '%s: is still alive' % addr      else:        print '%s: did not respond ' % addr      self._queue.task_done() #unfinished tasks -= 1  def run(self):    for ip in self._ip_list:      self._queue.put(ip) #unfinished_tasks += 1    print '---------------------task begin------------------'    for i in range(self._thread_num):      thrd = threading.Thread(target=self.ping, args=(i + 1,))      #thrd.setDaemon(True)      thrd.start()    self._queue.join() # 主線程一直阻塞,一直等到Queue.unfiinshed_tasks == 0    print '---------------------task done-------------------'

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結》、《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱州市| 河北区| 海林市| 罗甸县| 香格里拉县| 双江| 铜川市| 南通市| 苍溪县| 和田县| 安多县| 梧州市| 营山县| 湟源县| 防城港市| 云和县| 虎林市| 禹城市| 和平县| 太康县| 桐梓县| 大丰市| 封丘县| 万源市| 绥德县| 衡阳县| 济源市| 綦江县| 海南省| 昌图县| 永兴县| 阿拉善右旗| 嘉义县| 清河县| 绥化市| 胶州市| 古浪县| 华安县| 额尔古纳市| 精河县| 长治市|