對(duì)python判斷ip是否可達(dá)的實(shí)例詳解
python中使用subprocess來(lái)使用shell
from __future__ import print_function import subprocess import threading def is_reachable(ip): if subprocess.call(["ping", "-c", "2", ip])==0:#只發(fā)送兩個(gè)ECHO_REQUEST包 print("{0} is alive.".format(ip)) else: print("{0} is unalive".format(ip)) if __name__ == "__main__": ips = ["www.baidu.com","192.168.0.1"] threads = [] for ip in ips: thr = threading.Thread(target=is_reachable, args=(ip,))#參數(shù)必須為tuple形式 thr.start()#啟動(dòng) threads.append(thr) for thr in threads: thr.join()
改良 :使用Queue來(lái)優(yōu)化(FIFO)
from __future__ import print_function import subprocess import threading from Queue import Queue from Queue import Empty def call_ping(ip): if subprocess.call(["ping", "-c", "2", ip])==0: print("{0} is reachable".format(ip)) else: print("{0} is unreachable".format(ip)) def is_reachable(q): try: while True: ip = q.get_nowait()#當(dāng)隊(duì)列為空,不等待 call_ping(ip) except Empty: pass def main(): q = Queue() args = ["www.baidu.com", "www.sohu.com", "192.168.0.1"] for arg in args: q.put(arg) threads = [] for i in range(10): thr = threading.Thread(target=is_reachable, args=(q,)) thr.start() threads.append(thr) for thr in threads: thr.join() if __name__ == "__main__": main()
以上這篇對(duì)python判斷ip是否可達(dá)的實(shí)例詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux 發(fā)郵件磁盤(pán)空間監(jiān)控(python)
這篇文章主要介紹了Linux發(fā)郵件磁盤(pán)空間監(jiān)控功能,python實(shí)現(xiàn),需要的朋友可以參考下2016-04-04python打包為linux可執(zhí)行文件的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于python打包為linux可執(zhí)行文件的詳細(xì)圖文教程,本文介紹的方法可以輕松地將Python代碼變成獨(dú)立的可執(zhí)行文件,需要的朋友可以參考下2024-02-02Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測(cè)值全為0.0的問(wèn)題)
這篇文章主要介紹了Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測(cè)值全為0.0的問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07詳解在OpenCV中實(shí)現(xiàn)的圖像標(biāo)注技術(shù)
圖像標(biāo)注在計(jì)算機(jī)視覺(jué)中很重要,計(jì)算機(jī)視覺(jué)是一種技術(shù),它允許計(jì)算機(jī)從數(shù)字圖像或視頻中獲得高水平的理解力,并以人類(lèi)的方式觀察和解釋視覺(jué)信息,本文將重點(diǎn)討論在OpenCV的幫助下創(chuàng)建這些注釋?zhuān)信d趣的朋友一起看看吧2022-06-06正確的理解和使用Django信號(hào)(Signals)
這篇文章主要介紹了如何正確的理解和使用Django信號(hào)(Signals),幫助大家更好的理解和學(xué)習(xí)是Django,感興趣的朋友可以了解下2021-04-04python實(shí)現(xiàn)對(duì)excel進(jìn)行數(shù)據(jù)剔除操作實(shí)例
python在數(shù)據(jù)分析這方便的介紹應(yīng)該不用多說(shuō)了,下面這篇文章主要給大家介紹了關(guān)于利用python實(shí)現(xiàn)對(duì)excel進(jìn)行數(shù)據(jù)剔除操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-12-12