Python基于多線程實現(xiàn)ping掃描功能示例
本文實例講述了Python基于多線程實現(xiàn)ping掃描功能。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*-
#! python2
import subprocess
from Queue import Queue
import threading
class 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-------------------'
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- Python測試網(wǎng)絡(luò)連通性示例【基于ping】
- Python實現(xiàn)ping指定IP的示例
- 使用python編寫udp協(xié)議的ping程序方法
- 利用python獲取Ping結(jié)果示例代碼
- python使用arcpy.mapping模塊批量出圖
- 利用Python腳本實現(xiàn)ping百度和google的方法
- 詳解duck typing鴨子類型程序設(shè)計與Python的實現(xiàn)示例
- Python實現(xiàn)快速多線程ping的方法
- python實現(xiàn)ping的方法
- python使用xmlrpclib模塊實現(xiàn)對百度google的ping功能
- python在windows下實現(xiàn)ping操作并接收返回信息的方法
- Python實現(xiàn)檢測服務(wù)器是否可以ping通的2種方法
- Python檢查ping終端的方法
相關(guān)文章
python 和c++實現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式
今天小編就為大家分享一篇python 和c++實現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
關(guān)于使用python對mongo多線程更新數(shù)據(jù)
這篇文章主要介紹了關(guān)于使用python對mongo多線程更新數(shù)據(jù),文中提供了詳細的代碼說明,實際使用時,需要根據(jù)具體情況進行調(diào)整和優(yōu)化,需要的朋友可以參考下2023-04-04
Python+AutoIt實現(xiàn)界面工具開發(fā)過程詳解
這篇文章主要介紹了Python+AutoIt實現(xiàn)界面工具開發(fā)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

