对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实现简单温度转换的方法
Mar 13 Python
Python实现自动为照片添加日期并分类的方法
Sep 30 Python
python+selenium实现登录账户后自动点击的示例
Dec 22 Python
pyqt5自定义信号实例解析
Jan 31 Python
Python 虚拟空间的使用代码详解
Jun 10 Python
windows下安装Python虚拟环境virtualenvwrapper-win
Jun 14 Python
从numpy数组中取出满足条件的元素示例
Nov 26 Python
基于keras输出中间层结果的2种实现方式
Jan 24 Python
PyCharm 2020 激活到 2100 年的教程
Mar 25 Python
使用Keras实现简单线性回归模型操作
Jun 12 Python
Django REST 异常处理详解
Jul 15 Python
移除Selenium中window.navigator.webdriver值
Jun 10 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生成随机字符串可指定纯数字、纯字母或者混合的
2014/04/18 PHP
thinkphp模板用法和内容输出实例
2014/11/28 PHP
smarty模板引擎从php中获取数据的方法
2015/01/22 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
2017/01/13 PHP
使用composer 安装 laravel框架的方法图文详解
2019/08/02 PHP
php面向对象基础详解【星际争霸游戏案例】
2020/01/23 PHP
nodejs之请求路由概述
2014/07/05 NodeJs
JavaScript+html5 canvas制作色彩斑斓的正方形效果
2016/01/27 Javascript
jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
2016/05/10 Javascript
jquery实现文字单行横移或翻转(上下、左右跳转)
2017/01/08 Javascript
5种JavaScript脚本加载的方式
2017/01/16 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
解决bootstrap下拉菜单点击立即隐藏bug的方法
2017/06/13 Javascript
Nodejs之TCP服务端与客户端聊天程序详解
2017/07/07 NodeJs
elemetUi 组件--el-upload实现上传Excel文件的实例
2017/10/27 Javascript
nodejs同步调用获取mysql数据时遇到的大坑
2019/03/02 NodeJs
Vue中Table组件Select的勾选和取消勾选事件详解
2019/03/19 Javascript
重学 JS:为啥 await 不能用在 forEach 中详解
2019/04/15 Javascript
Node.js实现批量下载图片简单操作示例
2020/01/18 Javascript
Python计算回文数的方法
2015/03/11 Python
python简单实现计算过期时间的方法
2015/06/09 Python
python的多重继承的理解
2017/08/06 Python
Python调用C语言的方法【基于ctypes模块】
2018/01/22 Python
高效使用Python字典的清单
2018/04/04 Python
Python实现注册、登录小程序功能
2018/09/21 Python
python地震数据可视化详解
2019/06/18 Python
Pandas库之DataFrame使用的学习笔记
2019/06/21 Python
Python 使用threading+Queue实现线程池示例
2019/12/21 Python
OpenCV利用python来实现图像的直方图均衡化
2020/10/21 Python
Python如何实现Paramiko的二次封装
2021/01/30 Python
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
印刷技术专业自荐信
2014/09/18 职场文书
关于CSS浮动与取消浮动的问题
2021/06/28 HTML / CSS
php去除数组中为0的元素的实例分析
2021/11/17 PHP