怎麼利用Python實現網路測試
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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

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

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

VS Code可以在Windows 8上運行,但體驗可能不佳。首先確保系統已更新到最新補丁,然後下載與系統架構匹配的VS Code安裝包,按照提示安裝。安裝後,注意某些擴展程序可能與Windows 8不兼容,需要尋找替代擴展或在虛擬機中使用更新的Windows系統。安裝必要的擴展,檢查是否正常工作。儘管VS Code在Windows 8上可行,但建議升級到更新的Windows系統以獲得更好的開發體驗和安全保障。

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

VS Code 可用於編寫 Python,並提供許多功能,使其成為開發 Python 應用程序的理想工具。它允許用戶:安裝 Python 擴展,以獲得代碼補全、語法高亮和調試等功能。使用調試器逐步跟踪代碼,查找和修復錯誤。集成 Git,進行版本控制。使用代碼格式化工具,保持代碼一致性。使用 Linting 工具,提前發現潛在問題。

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。
