Python执行ping操作的简单方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python执行ping操作的简单方法

田辛 | 田豆芽   2023-03-20 我要评论

1 基本信息

- 模块主页:[github]
- 类型:#第三方库

2 安装方法

pip install pythonping

3 一般使用

from pythonping import ping

@timer  
def case1():  
    return (ping('www.baidu.com', verbose=True))  
@timer  
def case2():  
    return ping('www.baidu.com', verbose=False)

verbose=True:意味着输出平的执行过程,测试Case1:

--------------------------------------------------------------------------------
[case1] start at 2023-02-12 07:45:48.366523
Reply from 110.242.68.4, 29 bytes in 11.75ms
Reply from 110.242.68.4, 29 bytes in 11.33ms
Reply from 110.242.68.4, 29 bytes in 11.32ms
Reply from 110.242.68.4, 29 bytes in 11.33ms
函数[case1]执行时间为:0.05608487129211426
函数[case1]执行结果为:
Reply from 110.242.68.4, 29 bytes in 11.75ms
Reply from 110.242.68.4, 29 bytes in 11.33ms
Reply from 110.242.68.4, 29 bytes in 11.32ms
Reply from 110.242.68.4, 29 bytes in 11.33ms

Round Trip Times min/avg/max is 11.32/11.43/11.75 ms
[case1] end at 2023-02-12 07:45:48.422608
--------------------------------------------------------------------------------

 verbose=False:意味着不输出执行过程,测试Case2:

--------------------------------------------------------------------------------
[case2] start at 2023-02-12 07:45:48.422608
函数[case2]执行时间为:0.04709315299987793
函数[case2]执行结果为:
Reply from 110.242.68.4, 29 bytes in 11.41ms
Reply from 110.242.68.4, 29 bytes in 11.56ms
Reply from 110.242.68.4, 29 bytes in 12.15ms
Reply from 110.242.68.4, 29 bytes in 11.75ms

Round Trip Times min/avg/max is 11.41/11.72/12.15 ms
[case2] end at 2023-02-12 07:45:48.470690
--------------------------------------------------------------------------------

可以看出,case1有执行过程,但是case2没有。

4 ping的返回值

ping的返回值是一个ResponseList对象,既然叫做List那么肯定是可以枚举的。 我们来测试一下:

@timer  
def case3():  
    print("STEP 1: ping www.baidu.com")  
    ping_rst = ping('www.baidu.com', verbose=False)  
  
    print("ping返回值的数据类型是:%s" % type(ping_rst))  
  
    print("STEP 2: 遍历ResponseList对象的所有属性")  
    for ping_item in ping_rst.__dict__:  
        print("[%s]:%s" % (ping_item, ping_rst.__dict__[ping_item]))  
  
    print("STEP 3: 遍历Response对象的所有属性")  
    cnt = 1  
    for response_item in ping_rst:  
        print("STEP 3-%s. Resoonse对象" % cnt)  
        cnt += 1  
        for item in response_item.__dict__:  
            print("[%s]:%s" % (item, response_item.__dict__[item]))

4.1 返回值类型以及常用属性

在例程3中, 第一步是执行ping函数,并且取得他的返回值。

第一步的返回结果是:

STEP 1: ping www.baidu.com
ping返回值的数据类型是:<class 'pythonping.executor.ResponseList'>

以上表明, 返回的是pythonping内部定义的一个对象。 既然如此,我们接下来看看一下这个对象的属性,执行结果如下:

STEP 2: 遍历ResponseList对象的所有属性
[_responses]:[Reply from 110.242.68.4, 29 bytes in 11.68ms, Reply from 110.242.68.4, 29 bytes in 11.39ms, Reply from 110.242.68.4, 29 bytes in 11.51ms, Reply from 110.242.68.4, 29 bytes in 11.82ms]
[stats_packets_sent]:4
[stats_packets_returned]:4
[verbose]:False
[output]:<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
[rtt_avg]:0.01159732499945676
[rtt_min]:0.011385000019799918
[rtt_max]:0.011815299978479743

在这次遍历中,有一个内部属性_responses实际上就是4此ping请求的信息。 这里我们日常使用比较多的。就是三个rtt开头的值,只是用方法直接作为属性读取即可。
关于RTT这个缩写,我读了pythonping的源码,并灭有给出详细的解释。 于是我请教了ChatGPT,它的回答是:

RTT代表往返时间(Round Trip Time),即数据包从源传输到目的地再返回所需的时间。RTT通常用于测量网络延迟和网络连接质量。在网络通信的上下文中,RTT是指从发送请求到接收相应响应之间经过的时间。

4.2 ResponseList中的每个Response对象的属性

这部分内容在STEP 3中, 我遍历了所有的属性:

STEP 3-1. Resoonse对象
[message]:45 00 00 1d c1 52 00 00 35 01 1e 03 6e f2 44 04 c0 a8 32 ec 00 00 46 f9 7f 06 01 00 39
[time_elapsed]:0.01167790000909008
[source_request]:08 00 3e f9 7f 06 01 00 39
[repr_format]:legacy
STEP 3-2. Resoonse对象
......

这里面有4个属性:

  • message: 发送的内容
  • time_elapsed: 时间
  • source_request:接收的信息
  • repr_format: 如何将返回值变为文字列。 有两个可能的属性legacy以及None

4.3 pythonping.ping() 方法的常用形参包括:

  • hostname: 目标主机的域名或 IP 地址
  • size: 发送的数据的大小,以字节为单位。默认为 56 字节
  • count: 要发送的请求的数量。默认为 4 次
  • timeout: 超时时间,以秒为单位。默认为 1s
  • verbose: 布尔值,用于指示是否显示详细的输出。默认为 False

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们