# -*- 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获取PING结果的更多相关文章

  1. 使用shell/python获取hostname/fqdn释疑

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  2. 使用shell/python获取hostname/fqdn释疑(转)

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  3. HCNP学习笔记之ICMP协议与ping原理以及用Python实现ping

    一.ICMP协议分析 ICMP:Internet控制报文协议.由于IP协议并不是一个可靠的协议,它不保证数据被成功送达,那么,如何才能保证数据的可靠送达呢? 这里就需要使用到一个重要的协议模块ICMP ...

  4. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  5. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  6. python获取文件大小

    python获取文件大小 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- import os # 字节bytes转化kb\m\g def formatSiz ...

  7. python 获取一个列表有多少连续列表

    python 获取一个列表有多少连续列表 例如 有列表 [1,2,3] 那么连续列表就是 [1,2],[2,3],[1,2,3] 程序实现如下: 运行结果:

  8. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  9. Python获取目录、文件的注意事项

    Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...

随机推荐

  1. 架构:Screaming Architecture(转载)

    Imagine that you are looking at the blueprints of a building. This document, prepared by an architec ...

  2. spring 5.0.1.RELEASE官方任然不支持velocity(平台升级)

    官方说明: Dear Spring community, It is my pleasure to announce that Spring Framework 5.0.1 is available ...

  3. Linux为sh脚本文件添加执行权限

    chmod是权限管理命令change the permissions mode of a file的缩写..u代表所有者,x代表执行权限. + 表示增加权限.chmod u+x file.sh 就表示 ...

  4. 蓝精灵:寻找神秘村Smurfs: The Lost Village迅雷下载

    蓝妹妹(黛米·洛瓦托 Demi Lovato 配音)发现了一张遗落的地图,由此引发精灵们对于神秘村庄真实性的猜想.于是,满怀好奇心的蓝妹妹与聪聪(丹尼·朴迪 Danny Pudi 配音).笨笨(杰克· ...

  5. 新闻编辑室第一季/全集The Newsroom迅雷下载

    第一季 The Newsroom Season 1 (2012)看点:频出佳剧的HBO这次将目光对准了新闻间这个话题多,故事更多的“小”地方.该剧讲述的是一家虚构的有线电视新闻网ACN的故事,由阿伦· ...

  6. Asp.Net Mvc3.0(MEF依赖注入理论)

    前言 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)等.MEF为开发人员提供了一个工具,让我们 ...

  7. Netty 中 IOException: Connection reset by peer 与 java.nio.channels.ClosedChannelException: null

    最近发现系统中出现了很多 IOException: Connection reset by peer 与 ClosedChannelException: null 深入看了看代码, 做了些测试, 发现 ...

  8. Java Type Inference (类型推断)

    public class Test2 { public static void main(String[] args) { ArrayList<String> list = newArra ...

  9. SVN服务器搭建和使用(转)

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...

  10. Libnids(Library Network Intrusion Detection System) .

    Libnids(Library Network Intrusion Detection System)是一个网络入侵检测开发的专业编程接口.它实现了基于网络的入侵检测系统的基本框架,并提供了一些基本的 ...