对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实现代码行数统计示例分享
Feb 10 Python
Python切片知识解析
Mar 06 Python
python实现拓扑排序的基本教程
Mar 11 Python
基于Python List的赋值方法
Jun 23 Python
使用Python正则表达式操作文本数据的方法
May 14 Python
浅谈PySpark SQL 相关知识介绍
Jun 14 Python
Python笔试面试题小结
Sep 07 Python
python实现KNN分类算法
Oct 16 Python
python 19个值得学习的编程技巧
Aug 15 Python
Pycharm自带Git实现版本管理的方法步骤
Sep 18 Python
Python Pandas模块实现数据的统计分析的方法
Jun 24 Python
Python 数据可视化工具 Pyecharts 安装及应用
Apr 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下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
2011/09/19 PHP
php函数间的参数传递(值传递/引用传递)
2013/09/23 PHP
PHP中使用file_get_contents post数据代码例子
2015/02/13 PHP
PHP区块查询实现方法分析
2018/05/12 PHP
laravel实现查询最后执行的一条sql语句的方法
2019/10/09 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
js 创建书签小工具之理论
2011/02/25 Javascript
JavaScript和CSS通过expression实现Table居中显示
2013/06/28 Javascript
javascript禁用键盘功能键让右击及其他键无效
2013/10/09 Javascript
javascript 事件处理示例分享
2014/12/31 Javascript
利用JavaScript脚本实现滚屏效果的方法
2015/07/07 Javascript
jquery  实现轮播图详解及实例代码
2016/10/12 Javascript
浅谈jQuery绑定事件会叠加的解决方法和心得总结
2016/10/26 Javascript
vue-better-scroll 的使用实例代码详解
2018/12/03 Javascript
vue 实现input表单元素的disabled示例
2019/10/28 Javascript
微信小程序vant弹窗组件的实现方式
2020/02/21 Javascript
原生JS实现贪吃蛇小游戏
2020/03/09 Javascript
深入了解JavaScript词法作用域
2020/07/29 Javascript
通过实例浅析Python对比C语言的编程思想差异
2015/08/30 Python
使用Python的PIL模块来进行图片对比
2016/02/18 Python
python实现批量图片格式转换
2020/06/16 Python
使用python 打开文件并做匹配处理的实例
2019/01/02 Python
Tensorflow加载Vgg预训练模型操作
2020/05/26 Python
英国IT硬件供应商,定制游戏PC:Mesh Computers
2019/03/28 全球购物
英国网上自行车商店:Tredz Bikes
2019/10/29 全球购物
社区十八大感言
2014/01/19 职场文书
电力公司个人求职信范文
2014/02/04 职场文书
2014春晚主持词
2014/03/25 职场文书
班级文化标语
2014/06/23 职场文书
525心理活动总结
2014/07/04 职场文书
学生会竞选演讲稿纪检部
2014/08/25 职场文书
食品药品安全责任书
2015/05/11 职场文书
男人帮观后感
2015/06/18 职场文书
会计入职心得体会
2016/01/22 职场文书
大学生奖学金获奖感言(范文)
2019/08/15 职场文书
MySQL 慢查询日志深入理解
2021/04/22 MySQL