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

首頁 > 編程 > Python > 正文

對python判斷ip是否可達的實例詳解

2020-02-16 01:01:47
字體:
來源:轉載
供稿:網友

python中使用subprocess來使用shell

關于threading的用法

from __future__ import print_functionimport subprocessimport threadingdef is_reachable(ip):  if subprocess.call(["ping", "-c", "2", ip])==0:#只發送兩個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,))#參數必須為tuple形式    thr.start()#啟動    threads.append(thr)  for thr in threads:    thr.join()

改良 :使用Queue來優化(FIFO)

from __future__ import print_functionimport subprocessimport threadingfrom Queue import Queuefrom Queue import Emptydef 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()#當隊列為空,不等待      call_ping(ip)  except Empty:    passdef 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()

以上這篇對python判斷ip是否可達的實例詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 出国| 阳原县| 图木舒克市| 合作市| 博客| 慈利县| 内黄县| 从江县| 清河县| 上饶县| 兴安县| 白河县| 兴仁县| 武威市| 宜兰县| 玛曲县| 巧家县| 宜州市| 济宁市| 三门县| 永康市| 孙吴县| 四川省| 简阳市| 万荣县| 敦化市| 礼泉县| 紫云| 晋宁县| 凤翔县| 新宾| 会宁县| 即墨市| 福州市| 榆林市| 安丘市| 马龙县| 东平县| 拜泉县| 中西区| 汶川县|