对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 相关文章推荐
python模拟登陆阿里妈妈生成商品推广链接
Apr 03 Python
Python中Continue语句的用法的举例详解
May 14 Python
Python文件夹与文件的相关操作(推荐)
Jul 25 Python
50行Python代码实现人脸检测功能
Jan 23 Python
python实现机器人行走效果
Jan 29 Python
Python OpenCV中的resize()函数的使用
Jun 20 Python
django的ORM操作 删除和编辑实现详解
Jul 24 Python
django基础学习之send_mail功能
Aug 07 Python
一行Python代码制作动态二维码的实现
Sep 09 Python
python 实现方阵的对角线遍历示例
Nov 29 Python
将python文件打包exe独立运行程序方法详解
Feb 12 Python
Python os模块常用方法和属性总结
Feb 20 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笔记之:AOP的应用
2013/04/24 PHP
php上传大文件失败的原因及应对策略
2015/10/20 PHP
thinkPHP通用控制器实现方法示例
2017/11/23 PHP
Yii框架实现对数据库的CURD操作示例
2019/09/03 PHP
js控制CSS样式属性语法对照表
2012/12/11 Javascript
node.js中使用node-schedule实现定时任务实例
2014/06/03 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
处理文本部分内容的TextRange对象应用实例
2014/07/29 Javascript
小议JavaScript中Generator和Iterator的使用
2015/07/29 Javascript
浅谈JavaScript的Polymer框架中的behaviors对象
2015/07/29 Javascript
JS实现可自定义大小,可双击关闭的弹出层效果
2015/10/16 Javascript
JavaScript与HTML的结合方法详解
2015/11/23 Javascript
基于jquery实现即时检查格式是否正确的表单
2016/05/06 Javascript
关于预加载InstantClick的问题解决方法
2017/09/12 Javascript
nodejs的路径问题的解决
2018/06/30 NodeJs
Vue2 监听属性改变watch的实例代码
2018/08/27 Javascript
详解js中Array的方法及技巧
2018/09/12 Javascript
深入理解react-router 路由的实现原理
2018/09/26 Javascript
Python检测QQ在线状态的方法
2015/05/09 Python
python调用接口的4种方式代码实例
2019/11/19 Python
python FTP批量下载/删除/上传实例
2019/12/22 Python
解决TensorFlow模型恢复报错的问题
2020/02/06 Python
pyCharm 实现关闭代码检查
2020/06/09 Python
基于python实现删除指定文件类型
2020/07/21 Python
Python爬虫之Selenium中frame/iframe表单嵌套页面
2020/12/04 Python
移动端html5判断是否滚动到底部并且下拉加载
2019/11/19 HTML / CSS
慕尼黑山地运动、户外服装和体育用品专家:Sporthaus Schuster
2019/08/27 全球购物
局部内部类是否可以访问非final变量?
2013/04/20 面试题
硕士生工作推荐信
2014/03/07 职场文书
产品销售计划书
2014/05/04 职场文书
2014国庆节主题活动方案:快乐的国庆节
2014/09/16 职场文书
公安机关查摆剖析材料
2014/10/10 职场文书
离婚案件答辩状
2015/05/22 职场文书
交通事故案件代理词
2015/05/23 职场文书
商场广播稿范文
2015/08/19 职场文书
总结Python变量的相关知识
2021/06/28 Python