網(wǎng)站首頁 編程語言 正文
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:意味著輸出平的執(zhí)行過程,測試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
函數(shù)[case1]執(zhí)行時間為:0.05608487129211426
函數(shù)[case1]執(zhí)行結(jié)果為:
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.33msRound 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:意味著不輸出執(zhí)行過程,測試Case2:
--------------------------------------------------------------------------------
[case2] start at 2023-02-12 07:45:48.422608
函數(shù)[case2]執(zhí)行時間為:0.04709315299987793
函數(shù)[case2]執(zhí)行結(jié)果為:
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.75msRound Trip Times min/avg/max is 11.41/11.72/12.15 ms
[case2] end at 2023-02-12 07:45:48.470690
--------------------------------------------------------------------------------
可以看出,case1有執(zhí)行過程,但是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返回值的數(shù)據(jù)類型是:%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中, 第一步是執(zhí)行ping函數(shù),并且取得他的返回值。
第一步的返回結(jié)果是:
STEP 1: ping www.baidu.com
ping返回值的數(shù)據(jù)類型是:<class 'pythonping.executor.ResponseList'>
以上表明, 返回的是pythonping內(nèi)部定義的一個對象。 既然如此,我們接下來看看一下這個對象的屬性,執(zhí)行結(jié)果如下:
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
在這次遍歷中,有一個內(nèi)部屬性_responses實際上就是4此ping請求的信息。 這里我們?nèi)粘J褂帽容^多的。就是三個rtt開頭的值,只是用方法直接作為屬性讀取即可。
關(guān)于RTT這個縮寫,我讀了pythonping的源碼,并滅有給出詳細的解釋。 于是我請教了ChatGPT,它的回答是:
RTT代表往返時間(Round Trip Time),即數(shù)據(jù)包從源傳輸?shù)侥康牡卦俜祷厮璧臅r間。RTT通常用于測量網(wǎng)絡(luò)延遲和網(wǎng)絡(luò)連接質(zhì)量。在網(wǎng)絡(luò)通信的上下文中,RTT是指從發(fā)送請求到接收相應(yīng)響應(yīng)之間經(jīng)過的時間。
4.2 ResponseList中的每個Response對象的屬性
這部分內(nèi)容在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: 發(fā)送的內(nèi)容
- time_elapsed: 時間
- source_request:接收的信息
- repr_format: 如何將返回值變?yōu)槲淖至小?有兩個可能的屬性legacy以及None
4.3 pythonping.ping() 方法的常用形參包括:
- hostname: 目標主機的域名或 IP 地址
- size: 發(fā)送的數(shù)據(jù)的大小,以字節(jié)為單位。默認為 56 字節(jié)
- count: 要發(fā)送的請求的數(shù)量。默認為 4 次
- timeout: 超時時間,以秒為單位。默認為 1s
- verbose: 布爾值,用于指示是否顯示詳細的輸出。默認為 False
原文鏈接:https://blog.csdn.net/u013589130/article/details/128992411
相關(guān)推薦
- 2022-11-11 python?使用第三方庫requests-toolbelt?上傳文件流的示例_python
- 2022-04-30 DataGridView自動設(shè)定列寬和行高_C#教程
- 2024-03-04 新版ECharts實現(xiàn)“暫無數(shù)據(jù)”的完美解決方案
- 2022-07-06 C語言深入探究水仙花數(shù)與變種水仙花數(shù)代碼_C 語言
- 2022-08-15 當添加一個鍵值對元素時,HashMap發(fā)生了什么?
- 2022-11-28 基于GORM實現(xiàn)CreateOrUpdate方法詳解_Golang
- 2022-10-03 Pandas數(shù)據(jù)集的分塊讀取的實現(xiàn)_python
- 2023-10-26 在el-table中根據(jù)判斷不同值顯示對應(yīng)文本
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支