使用python監(jiān)測(cè)網(wǎng)絡(luò)連接和網(wǎng)速的實(shí)現(xiàn)代碼
大家好,網(wǎng)絡(luò)連接和網(wǎng)速在工作中非常重要,本文將介紹如何使用Python程序來(lái)監(jiān)測(cè)互聯(lián)網(wǎng)連接的速度和中斷情況。
1.導(dǎo)入所需庫(kù)文件
首先,需要導(dǎo)入以下庫(kù):
import speedtest import time from datetime import datetime
這里選擇了第三方庫(kù)speedtest-cli來(lái)執(zhí)行實(shí)際的下載和上傳速度測(cè)試。
2.創(chuàng)建循環(huán)變量
然后,創(chuàng)建一個(gè)名為i的變量,并將其值設(shè)為True。
i = True
這只是為了在主函數(shù)運(yùn)行時(shí)保持循環(huán),只要i為True,程序就會(huì)一直執(zhí)行下去。
3.執(zhí)行主要功能的函數(shù)
def run_prog():
print("Please wait while GPisc runs it's checks...\n")
try:
now = datetime.now()
time_date = now.strftime("%d/%m/%Y %H:%M:%S")
st = speedtest.Speedtest()
server = st.get_best_server()
server_location = server.get("name")
host = server.get("host")
dl_speed = st.download()
ul_speed = st.upload()
print(f'SERVER: {host} | LOCATION: {server_location}\n')
with open('DL_LOG.txt', 'a') as log:
log.writelines(f'{time_date} - DOWNLOAD SPEED: {dl_speed}\n')
print(f'DOWNLOAD SPEED: {dl_speed}\n')
with open('UL_LOG.txt', 'a') as log:
log.writelines(f'{time_date} - UPLOAD SPEED: {ul_speed}\n')
print(f'UPLOAD SPEED: {ul_speed}\n')
except:
with open('NO_NET_LOG.txt', 'a') as log:
now = datetime.now()
time_date = now.strftime("%d/%m/%Y %H:%M:%S")
log.writelines(f'{time_date} - CONNECTION LOST!\n')
print('CONNECTION LOST!\n')上述代碼是執(zhí)行所有工作的函數(shù)。run_prog函數(shù)通過(guò)選擇測(cè)試時(shí)的最佳服務(wù)器來(lái)運(yùn)行下載和上傳速度測(cè)試,然后將結(jié)果記錄在3個(gè).txt文件中 - DL_LOG.txt、UL_LOG.txt和NO_NET_LOG.txt。
4.結(jié)果記錄和日志
日志中的數(shù)據(jù)如下所示:
05/08/2022 23:18:52 - DOWNLOAD SPEED: 51351410.36969972
05/08/2022 23:20:14 - DOWNLOAD SPEED: 54115624.477457255
05/08/2022 23:21:31 - DOWNLOAD SPEED: 50422315.83706379
05/08/2022 23:22:53 - DOWNLOAD SPEED: 48712675.779672034
05/08/2022 23:18:52 - UPLOAD SPEED: 34615688.56533798
05/08/2022 23:20:14 - UPLOAD SPEED: 14661625.766184961
05/08/2022 23:21:31 - UPLOAD SPEED: 36268210.76668299
05/08/2022 23:22:53 - UPLOAD SPEED: 34678387.95052972
25/07/2022 21:17:30 - CONNECTION LOST!
25/07/2022 21:18:30 - CONNECTION LOST!
25/07/2022 21:19:31 - CONNECTION LOST!
25/07/2022 21:20:28 - CONNECTION LOST!
如上所示,這里沒(méi)有將字節(jié)結(jié)果轉(zhuǎn)換為Mbps,因?yàn)橄MY(jié)果盡可能準(zhǔn)確。如果需要,可以使用hurry.filesize 0.9(https://pypi.org/project/hurry.filesize/)來(lái)進(jìn)行轉(zhuǎn)換。
run_prog函數(shù)使用try和except來(lái)捕獲除了速度結(jié)果之外的任何內(nèi)容,這顯然是沒(méi)有連接的情況,因?yàn)槿绻麩o(wú)法連接到速度測(cè)試服務(wù)器,程序?qū)⑹?,然后?xiě)入無(wú)連接日志。
while i:
run_prog()
time.sleep(60)
print('**********************************************\n')上述代碼執(zhí)行run_prog函數(shù),然后等待60秒再次運(yùn)行(這就是為什么創(chuàng)建了i = True)。
程序在每次執(zhí)行測(cè)試時(shí)都會(huì)在控制臺(tái)打印輸出。
5.主函數(shù)循環(huán)
以下是完整的腳本:
import speedtest
import time
from datetime import datetime
i = True
def run_prog():
print("Please wait while GPisc runs it's checks...\n")
try:
now = datetime.now()
time_date = now.strftime("%d/%m/%Y %H:%M:%S")
st = speedtest.Speedtest()
server = st.get_best_server()
server_location = server.get("name")
host = server.get("host")
dl_speed = st.download()
ul_speed = st.upload()
print(f'SERVER: {host} | LOCATION: {server_location}\n')
with open('DL_LOG.txt', 'a') as log:
log.writelines(f'{time_date} - DOWNLOAD SPEED: {dl_speed}\n')
print(f'DOWNLOAD SPEED: {dl_speed}\n')
with open('UL_LOG.txt', 'a') as log:
log.writelines(f'{time_date} - UPLOAD SPEED: {ul_speed}\n')
print(f'UPLOAD SPEED: {ul_speed}\n')
except:
with open('NO_NET_LOG.txt', 'a') as log:
now = datetime.now()
time_date = now.strftime("%d/%m/%Y %H:%M:%S")
log.writelines(f'{time_date} - CONNECTION LOST!\n')
print('CONNECTION LOST!\n')
while i:
run_prog()
time.sleep(60)
print('**********************************************\n')到此這篇關(guān)于使用python監(jiān)測(cè)網(wǎng)絡(luò)連接和網(wǎng)速的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)python監(jiān)測(cè)網(wǎng)絡(luò)連接和網(wǎng)速內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python的ping網(wǎng)絡(luò)狀態(tài)監(jiān)測(cè)的實(shí)現(xiàn)(含多IP)
- linux系統(tǒng)使用python監(jiān)測(cè)網(wǎng)絡(luò)接口獲取網(wǎng)絡(luò)的輸入輸出
- 基于python實(shí)現(xiàn)鼠標(biāo)實(shí)時(shí)坐標(biāo)監(jiān)測(cè)
- Python實(shí)現(xiàn)實(shí)時(shí)監(jiān)測(cè)可視化數(shù)據(jù)大屏
- Python實(shí)現(xiàn)日志實(shí)時(shí)監(jiān)測(cè)的示例詳解
相關(guān)文章
Python數(shù)據(jù)處理工具之Pandas的使用詳細(xì)指南
Pandas 是Python中最流行的數(shù)據(jù)處理和分析庫(kù)之一,它提供了高效、靈活且易于使用的數(shù)據(jù)結(jié)構(gòu),使得數(shù)據(jù)清洗、分析和處理變得簡(jiǎn)單直觀,下面小編就和大家詳細(xì)介紹一下它的具體應(yīng)用吧2026-05-05
python 實(shí)現(xiàn)列表的切片操作允許索引超出范圍
這篇文章主要介紹了python 實(shí)現(xiàn)列表的切片操作允許索引超出范圍,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python爬取網(wǎng)易云音樂(lè)熱門(mén)評(píng)論
本文將詳細(xì)介紹了Python獲取網(wǎng)易云音樂(lè)熱門(mén)評(píng)論的實(shí)例。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03
Python入門(mén)Anaconda和Pycharm的安裝和配置詳解
這篇文章主要介紹了Python入門(mén)Anaconda和Pycharm的安裝和配置詳解,文章通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python Pillow庫(kù)詳細(xì)介紹與代碼示例
Python Pillow 庫(kù)是一個(gè)強(qiáng)大的圖像處理工具,是經(jīng)典 PIL(Python Imaging Library)庫(kù)的現(xiàn)代分支,以下是對(duì) Pillow 庫(kù)的全面講解和完整代碼示例,需要的朋友可以參考下2025-04-04
macbook如何徹底刪除python的實(shí)現(xiàn)方法
本文主要介紹了macbook如何徹底刪除python的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Python實(shí)現(xiàn)代碼統(tǒng)計(jì)工具(終極篇)
這篇文章主要介紹了Python實(shí)現(xiàn)代碼統(tǒng)計(jì)工具的相關(guān)資料,供大家參考,感興趣的小伙伴們可以參考一下2016-07-07
Python回調(diào)函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python回調(diào)函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python回調(diào)函數(shù)的定義、功能及相關(guān)使用技巧,需要的朋友可以參考下2015-07-07

