对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创建xml的方法
Mar 10 Python
python实现支持目录FTP上传下载文件的方法
Jun 03 Python
Python for循环生成列表的实例
Jun 15 Python
Python使用pyodbc访问数据库操作方法详解
Jul 05 Python
在python里协程使用同步锁Lock的实例
Feb 19 Python
Python GUI编程 文本弹窗的实例
Jun 11 Python
PyQt5 多窗口连接实例
Jun 19 Python
Python使用字典实现的简单记事本功能示例
Aug 15 Python
python实现批量文件重命名
Oct 31 Python
Python面向对象之多态原理与用法案例分析
Dec 30 Python
Python unittest工作原理和使用过程解析
Feb 24 Python
使用tensorflow根据输入更改tensor shape
Jun 23 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远程连接MYSQL数据库非常慢的解决方法
2008/07/05 PHP
php中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
php 使用file_get_contents读取大文件的方法
2014/11/13 PHP
关于PHP通用返回值设置方法
2017/03/31 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
javascript比较文档位置
2008/04/08 Javascript
javascript 动态数据下的锚点错位问题解决方法
2008/12/24 Javascript
简单的js分页脚本
2009/05/21 Javascript
离开页面时检测表单元素是否被修改,提示保存的js代码
2010/08/25 Javascript
js实现鼠标拖动图片并兼容IE/FF火狐/谷歌等主流浏览器
2013/06/06 Javascript
AngularJS基础 ng-mouseover 指令简单示例
2016/08/02 Javascript
利用node.js搭建简单web服务器的方法教程
2017/02/20 Javascript
js放到head中失效的原因与解决方法
2017/03/07 Javascript
jQuery复合事件用法示例
2017/06/10 jQuery
微信小程序如何使用云开发
2019/05/17 Javascript
p5.js临摹旋转爱心
2019/10/23 Javascript
javascript实现弹出层效果
2019/12/10 Javascript
python在控制台输出进度条的方法
2015/06/20 Python
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
python检测服务器端口代码实例
2019/08/31 Python
Python使用Turtle库绘制一棵西兰花
2019/11/23 Python
Tensorflow Summary用法学习笔记
2020/01/10 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
2020/03/08 Python
python代码实现图书管理系统
2020/11/30 Python
HTML5图片层叠的实现示例
2020/07/07 HTML / CSS
工商管理专业实习大学生自我鉴定
2013/09/19 职场文书
缴纳养老保险的证明
2014/01/10 职场文书
最新个人职业生涯规划书
2014/01/22 职场文书
营销总经理岗位职责
2014/02/02 职场文书
中学校庆方案
2014/03/17 职场文书
公司爱心捐款倡议书
2014/05/14 职场文书
清正廉洁演讲稿
2014/05/22 职场文书
求职信范文大全
2014/05/26 职场文书
德育标兵事迹材料
2014/08/24 职场文书
导游词之镜泊湖
2019/12/09 职场文书
mysql 获取时间方式
2022/03/20 MySQL