使用python監(jiān)測網絡連接和網速的實現代碼
大家好,網絡連接和網速在工作中非常重要,本文將介紹如何使用Python程序來監(jiān)測互聯網連接的速度和中斷情況。
1.導入所需庫文件
首先,需要導入以下庫:
import speedtest import time from datetime import datetime
這里選擇了第三方庫speedtest-cli
來執(zhí)行實際的下載和上傳速度測試。
2.創(chuàng)建循環(huán)變量
然后,創(chuàng)建一個名為i
的變量,并將其值設為True
。
i = True
這只是為了在主函數運行時保持循環(huán),只要i
為True
,程序就會一直執(zhí)行下去。
3.執(zhí)行主要功能的函數
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í)行所有工作的函數。run_prog
函數通過選擇測試時的最佳服務器來運行下載和上傳速度測試,然后將結果記錄在3個.txt
文件中 - DL_LOG.txt
、UL_LOG.txt
和NO_NET_LOG.txt
。
4.結果記錄和日志
日志中的數據如下所示:
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!
如上所示,這里沒有將字節(jié)結果轉換為Mbps,因為希望結果盡可能準確。如果需要,可以使用hurry.filesize 0.9(https://pypi.org/project/hurry.filesize/)來進行轉換。
run_prog函數使用try和except來捕獲除了速度結果之外的任何內容,這顯然是沒有連接的情況,因為如果無法連接到速度測試服務器,程序將失敗,然后寫入無連接日志。
while i: run_prog() time.sleep(60) print('**********************************************\n')
上述代碼執(zhí)行run_prog
函數,然后等待60秒再次運行(這就是為什么創(chuàng)建了i = True
)。
程序在每次執(zhí)行測試時都會在控制臺打印輸出。
5.主函數循環(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')
到此這篇關于使用python監(jiān)測網絡連接和網速的實現代碼的文章就介紹到這了,更多相關python監(jiān)測網絡連接和網速內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django實現將views.py中的數據傳遞到前端html頁面,并展示
這篇文章主要介紹了Django實現將views.py中的數據傳遞到前端html頁面并展示,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python 插入Null值數據到Postgresql的操作
這篇文章主要介紹了python 插入Null值數據到Postgresql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03