利用python获取Ping结果示例代码


Posted in Python onJuly 06, 2017

前言

本文主要跟大家分享了关于利用python获取Ping结果的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧。

示例代码:

# -*- coding: utf-8 -*-

import subprocess
import re

def get_ping_result(ip_address):
 p = subprocess.Popen(["ping.exe", ip_address], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
 out = p.stdout.read().decode('gbk')
 
 reg_receive = '已接收 = \d'
 match_receive = re.search(reg_receive, out)
 
 receive_count = -1
 
 if match_receive:
  receive_count = int(match_receive.group()[6:])
 
 if receive_count > 0: #接受到的反馈大于0,表示网络通
  reg_min_time = '最短 = \d+ms'
  reg_max_time = '最长 = \d+ms'
  reg_avg_time = '平均 = \d+ms'
  
  match_min_time = re.search(reg_min_time, out)
  min_time = int(match_min_time.group()[5:-2])
  
  match_max_time = re.search(reg_max_time, out)
  max_time = int(match_max_time.group()[5:-2])
  
  match_avg_time = re.search(reg_avg_time, out)
  avg_time = int(match_avg_time.group()[5:-2])
  
  return [receive_count, min_time, max_time, avg_time]
 else:
  print('网络不通,目标服务器不可达!')
  return [0, 9999, 9999, 9999]
  
if __name__ == '__main__':
 ping_result = get_ping_result('114.80.83.69')
 print(ping_result)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
Python linecache.getline()读取文件中特定一行的脚本
Sep 06 Python
深入解析Python中的上下文管理器
Jun 28 Python
windows及linux环境下永久修改pip镜像源的方法
Nov 28 Python
Python清空文件并替换内容的实例
Oct 22 Python
Python3.5面向对象编程图文与实例详解
Apr 24 Python
python的set处理二维数组转一维数组的方法示例
May 31 Python
python中update的基本使用方法详解
Jul 17 Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
Mar 04 Python
python3.7+selenium模拟淘宝登录功能的实现
May 26 Python
python获取整个网页源码的方法
Aug 03 Python
python用Configobj模块读取配置文件
Sep 26 Python
python实现excel公式格式化的示例代码
Dec 23 Python
Python中工作日类库Busines Holiday的介绍与使用
Jul 06 #Python
Python中动态检测编码chardet的使用教程
Jul 06 #Python
Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)
Jul 06 #Python
CentOS 7下Python 2.7升级至Python3.6.1的实战教程
Jul 06 #Python
Python中定时任务框架APScheduler的快速入门指南
Jul 06 #Python
Python如何快速实现分布式任务
Jul 06 #Python
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
Jul 06 #Python
You might like
从零开始的异世界生活:第二季延期后,B站上架了第二部剧场版
2020/05/06 日漫
php旋转图片90度的方法
2013/11/07 PHP
PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例
2018/06/09 PHP
优化网页之快速的呈现我们的网页
2007/06/29 Javascript
jquery $.ajax各个事件执行顺序
2010/10/15 Javascript
jQuery LigerUI 使用教程入门篇
2012/01/18 Javascript
jQuery的3种请求方式$.post,$.get,$.getJSON
2014/03/28 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
Javascript中replace()小结
2015/09/30 Javascript
AngularJS中$interval的用法详解
2016/02/02 Javascript
浅谈React + Webpack 构建打包优化
2018/01/23 Javascript
浅谈React之状态(State)
2018/09/19 Javascript
详解小程序不同页面之间通讯的解决方案
2018/11/23 Javascript
通过JS运行机制的角度说说作用域
2019/03/12 Javascript
jQuery+ajax实现批量删除功能完整示例
2019/06/06 jQuery
python 字典(dict)按键和值排序
2016/06/28 Python
Python matplotlib绘图可视化知识点整理(小结)
2018/03/16 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
2018/07/18 Python
Python Series从0开始索引的方法
2018/11/06 Python
Python发送邮件功能示例【使用QQ邮箱】
2018/12/04 Python
Marc Jacobs彩妆官网:Marc Jacobs Beauty
2017/07/03 全球购物
Clarks西班牙官方在线商店:clarks鞋
2019/05/03 全球购物
一道Delphi面试题
2016/10/28 面试题
机电专业体育教师求职信
2013/09/21 职场文书
工程部经理岗位职责
2013/12/08 职场文书
俄罗斯商务邀请函
2014/01/26 职场文书
小学生九一八纪念日83周年演讲稿500字
2014/09/17 职场文书
中层领导干部群众路线对照检查材料思想汇报
2014/10/02 职场文书
滞留工资返还协议书
2014/10/19 职场文书
2015年国庆节标语大全
2015/07/30 职场文书
行政后勤人员工作计划应该怎么写?
2019/08/16 职场文书
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
Go 自定义package包设置与导入操作
2021/05/06 Golang
pycharm安装深度学习pytorch的d2l包失败问题解决
2022/03/25 Python
Spring Data JPA框架Repository自定义实现
2022/04/28 Java/Android
在Windows Server 2012上安装 .NET Framework 3.5 所遇到的问题
2022/04/29 Servers