Speedtest CLI 專為軟體開發人員、系統管理員和電腦愛好者等打造,是 Ookla® 提供技術支援的首款正式 Linux 本機 Speedtest 應用程式。
Python語言編寫的Speedtest CLI可在命令列直接運行,實現網路速度測試。也可以直接作為python模組在python IDE中呼叫。
首先,先看看如何在python應用程式中進行調用,使用pip直接安裝。
pip install speedtest-cli
將該模組直接匯入到我們目前的程式碼區塊中。
import speedtest as spt
建立網路測試物件
spd = spt.Speedtest()
列印目前可用於測試的伺服器清單
from pprint import pprint pprint(spd.get_servers()) # {721.5702755019188: [{'cc': 'CN', # 'country': 'China', # 'd': 721.5702755019188, # 'host': 'speedtest1.he.chinamobile.com:8080', # 'id': '41912', # 'lat': '38.0428', # 'lon': '114.5149', # 'name': '石家庄', # 'sponsor': 'China Mobile Hebei Co., Ltd', # 'url': 'http://speedtest1.he.chinamobile.com:8080/speedtest/upload.php'}], # 776.2668436087947: [{'cc': 'CN', # 'country': 'China', # 'd': 776.2668436087947, # 'host': '5gtest.shangdu.com:8080', # 'id': '36646', # 'lat': '34.7466', # 'lon': '113.6253', # 'name': 'Zhengzhou', # 'sponsor': 'China Unicom HeNan 5G', # 'url': 'http://5gtest.shangdu.com:8080/speedtest/upload.php'}], # 1051.7168853741107: [{'cc': 'MN', # 'country': 'Mongolia', # 'd': 1051.7168853741107, # 'host': 'speedtest.gemnet.mn:8080', # 'id': '2853', # 'lat': '47.9200', # 'lon': '106.9200', # 'name': 'Ulaanbaatar', # 'sponsor': 'Gemnet LLC', # 'url': 'http://speedtest.gemnet.mn:8080/speedtest/upload.php'}, # {'cc': 'MN', # 'country': 'Mongolia', # 'd': 1051.7168853741107, # 'host': 'speedtest1.kewiko.mn:8080', # 'id': '30772', # 'lat': '47.9200', # 'lon': '106.9200', # 'name': 'Ulaanbaatar', # 'sponsor': 'Kewiko LLC', # 'url': 'http://speedtest1.kewiko.mn:8080/speedtest/upload.php'}], # 1339.1170164273938: [{'cc': 'CN', # 'country': 'China', # 'd': 1339.1170164273938, # 'host': '5gnanjing.speedtest.jsinfo.net:8080', # 'id': '26352', # 'lat': '32.0500', # 'lon': '118.7667', # 'name': 'Nanjing', # 'sponsor': 'China Telecom JiangSu 5G', # 'url': 'http://5gnanjing.speedtest.jsinfo.net:8080/speedtest/upload.php'}], # 1340.7612716854985: [{'cc': 'CN', # 'country': 'China', # 'd': 1340.7612716854985, # 'host': 'speedtest02.js165.com:8080', # 'id': '13704', # 'lat': '32.0602', # 'lon': '118.7968', # 'name': 'Nanjing', # 'sponsor': 'China Unicom', # 'url': 'http://speedtest02.js165.com:8080/speedtest/upload.php'}], # 1381.9129755930571: [{'cc': 'CN', # 'country': 'China', # 'd': 1381.9129755930571, # 'host': 'speedtest.zjmobile.com:8080', # 'id': '17320', # 'lat': '32.2069', # 'lon': '119.4490', # 'name': 'ZhenJiang', # 'sponsor': 'China Mobile JiangSu 5G', # 'url': 'http://speedtest.zjmobile.com:8080/speedtest/upload.php'}], # 1489.08809618835: [{'cc': 'RU', # 'country': 'Russia', # 'd': 1489.08809618835, # 'host': 'speedtest-ude.edinos.ru:8080', # 'id': '36254', # 'lat': '51.8336', # 'lon': '107.5840', # 'name': 'Ulan-Ude', # 'sponsor': 'EDINOS', # 'url': 'http://speedtest-ude.edinos.ru:8080/speedtest/upload.php'}], # 1542.170901504592: [{'cc': 'RU', # 'country': 'Russia', # 'd': 1542.170901504592, # 'host': 'speedtest.bteleport.ru:8080', # 'id': '18543', # 'lat': '52.2757', # 'lon': '104.3087', # 'name': 'Irkutsk', # 'sponsor': 'Baikal Teleport', # 'url': 'http://speedtest.bteleport.ru:8080/speedtest/upload.php'}, # {'cc': 'RU', # 'country': 'Russia', # 'd': 1542.170901504592, # 'host': 'speedtest-irkutsk.fttb.beeline.ru:8080', # 'id': '31472', # 'lat': '52.2757', # 'lon': '104.3087', # 'name': 'Irkutsk', # 'sponsor': 'Beeline', # 'url': 'http://speedtest-irkutsk.fttb.beeline.ru:8080/speedtest/upload.php'}]}
取得目前最佳的測試伺服器
spd.get_best_server() print('测试开始,请稍等...')
取得目前的下載速度
download = int(spd.download() / 1024 / 1024)
取得目前的上傳速度
upload = int(spd.upload() / 1024 / 1024) print(f'当前下载速度为:{str(download)} MB/s') print(f'当前上传速度为:{str(upload)} MB/s') print('测试已完成!')
列印出最終的返回結果
測試開始,請稍等...
目前下載速度為:12 MB/s
目前上傳速度為:13 MB/s
測試已完成!
以上是怎麼利用Python實現網路測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。

Python適合快速開發和數據處理,而C 適合高性能和底層控制。 1)Python易用,語法簡潔,適用於數據科學和Web開發。 2)C 性能高,控制精確,常用於遊戲和系統編程。

學習Python所需時間因人而異,主要受之前的編程經驗、學習動機、學習資源和方法及學習節奏的影響。設定現實的學習目標並通過實踐項目學習效果最佳。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),