使用python監(jiān)測(cè)網(wǎng)絡(luò)連接和網(wǎng)速的實(shí)現(xiàn)代碼
大家好,網(wǎng)絡(luò)連接和網(wǎng)速在工作中非常重要,本文將介紹如何使用Python程序來監(jiān)測(cè)互聯(lián)網(wǎng)連接的速度和中斷情況。
1.導(dǎo)入所需庫文件
首先,需要導(dǎo)入以下庫:
import speedtest import time from datetime import datetime
這里選擇了第三方庫speedtest-cli來執(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ù)通過選擇測(cè)試時(shí)的最佳服務(wù)器來運(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!
如上所示,這里沒有將字節(jié)結(jié)果轉(zhuǎn)換為Mbps,因?yàn)橄MY(jié)果盡可能準(zhǔn)確。如果需要,可以使用hurry.filesize 0.9(https://pypi.org/project/hurry.filesize/)來進(jìn)行轉(zhuǎn)換。
run_prog函數(shù)使用try和except來捕獲除了速度結(jié)果之外的任何內(nèi)容,這顯然是沒有連接的情況,因?yàn)槿绻麩o法連接到速度測(cè)試服務(wù)器,程序?qū)⑹。缓髮懭霟o連接日志。
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)文章
Django實(shí)現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示
這篇文章主要介紹了Django實(shí)現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面并展示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python3對(duì)拉勾數(shù)據(jù)進(jìn)行可視化分析的方法詳解
這篇文章主要給大家介紹了關(guān)于python3對(duì)拉勾數(shù)據(jù)進(jìn)行可視化分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
python 插入Null值數(shù)據(jù)到Postgresql的操作
這篇文章主要介紹了python 插入Null值數(shù)據(jù)到Postgresql的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
基于Python實(shí)現(xiàn)身份證信息識(shí)別功能
身份證是用于證明個(gè)人身份和身份信息的官方證件,在現(xiàn)代社會(huì)中,身份證被廣泛應(yīng)用于各種場(chǎng)景,如就業(yè)、教育、醫(yī)療、金融等,它包含了個(gè)人的基本信息,本文給大家介紹了如何基于Python實(shí)現(xiàn)身份證信息識(shí)別功能,感興趣的朋友可以參考下2024-01-01
Pycharm學(xué)生免費(fèi)專業(yè)版安裝教程的方法步驟
這篇文章主要介紹了Pycharm學(xué)生免費(fèi)專業(yè)版安裝教程的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

