对python判断ip是否可达的实例详解


Posted in Python onJanuary 31, 2019

python中使用subprocess来使用shell

from __future__ import print_function
import subprocess
import threading

def 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_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()#当队列为空,不等待
      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()

以上这篇对python判断ip是否可达的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
开始着手第一个Django项目
Jul 15 Python
Python抓取手机号归属地信息示例代码
Nov 28 Python
Python探索之爬取电商售卖信息代码示例
Oct 27 Python
Python实现PS滤镜中马赛克效果示例
Jan 20 Python
使用Python从零开始撸一个区块链
Mar 14 Python
浅谈numpy数组中冒号和负号的含义
Apr 18 Python
Python结合ImageMagick实现多张图片合并为一个pdf文件的方法
Apr 24 Python
在python中安装basemap的教程
Sep 20 Python
浅谈Python大神都是这样处理XML文件的
May 31 Python
vim自动补全插件YouCompleteMe(YCM)安装过程解析
Oct 21 Python
python将音频进行变速的操作方法
Apr 08 Python
python进行参数传递的方法
May 12 Python
对python:threading.Thread类的使用方法详解
Jan 31 #Python
python实现一个简单的ping工具方法
Jan 31 #Python
Python获取网段内ping通IP的方法
Jan 31 #Python
Python实现删除排序数组中重复项的两种方法示例
Jan 31 #Python
python重试装饰器的简单实现方法
Jan 31 #Python
Python实现合并两个有序链表的方法示例
Jan 31 #Python
Django 日志配置按日期滚动的方法
Jan 31 #Python
You might like
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
高性能WEB开发 flush让页面分块,逐步呈现 flush让页面分块,逐步呈现
2010/06/19 Javascript
js中判断数字\字母\中文的正则表达式 (实例)
2012/06/29 Javascript
jquery 表格排序、实时搜索表格内容(附图)
2014/05/19 Javascript
jquery中ajax使用error调试错误的方法
2015/02/08 Javascript
45个JavaScript编程注意事项、技巧大全
2015/02/11 Javascript
JavaScript jquery及AJAX小结
2016/01/24 Javascript
三个js循环的关键字示例(for与while)
2016/02/16 Javascript
Bootstrap布局组件应用实例讲解
2016/02/17 Javascript
js仿搜狐视频记录片列表展示效果
2020/05/30 Javascript
vuex操作state对象的实例代码
2018/04/25 Javascript
vue初始化动画加载的实例
2018/09/01 Javascript
深入理解NodeJS 多进程和集群
2018/10/17 NodeJs
使用mixins实现elementUI表单全局验证的解决方法
2019/04/02 Javascript
nodejs对项目下所有空文件夹创建gitkeep的方法
2019/08/02 NodeJs
布同 统计英文单词的个数的python代码
2011/03/13 Python
tornado捕获和处理404错误的方法
2014/02/26 Python
Python爬虫实现百度图片自动下载
2018/02/04 Python
Django2.1.3 中间件使用详解
2018/11/26 Python
Django 路由控制的实现
2019/07/17 Python
django 微信网页授权认证api的步骤详解
2019/07/30 Python
python飞机大战pygame碰撞检测实现方法分析
2019/12/17 Python
pytorch实现建立自己的数据集(以mnist为例)
2020/01/18 Python
Django框架配置mysql数据库实现过程
2020/04/22 Python
python 最简单的实现适配器设计模式的示例
2020/06/30 Python
Python 利用OpenCV给照片换底色的示例代码
2020/08/03 Python
使用Python解析Chrome浏览器书签的示例
2020/11/13 Python
X/HTML5 和 XHTML2
2008/10/17 HTML / CSS
师范生的个人求职信范文
2014/01/04 职场文书
竞争上岗演讲稿范文
2014/05/12 职场文书
销售人员求职信
2014/07/22 职场文书
2014市府办领导班子“四风问题”对照检查材料思想汇报
2014/09/24 职场文书
会计电算化实训报告
2014/11/04 职场文书
认真学习保证书
2015/02/26 职场文书
城南旧事观后感
2015/06/11 职场文书
《祁黄羊》教学反思
2016/02/20 职场文书