利用Python實現(xiàn)網(wǎng)絡(luò)測試的示例代碼
Speedtest CLI 專為軟件開發(fā)人員、系統(tǒng)管理員和計算機愛好者等打造,是 Ookla® 提供技術(shù)支持的首款正式 Linux 本機 Speedtest 應(yīng)用程序。
Speedtest CLI是使用python語言開發(fā)的,不僅可以直接在命令行運行。也可以作為python模塊在python IDE中直接調(diào)用。
首先,看一下如何在python應(yīng)用中進行調(diào)用,使用pip直接安裝。
pip install speedtest-cli
將該模塊直接導(dǎo)入到我們當(dāng)前的代碼塊中。
import speedtest as spt
創(chuàng)建網(wǎng)絡(luò)測試對象
spd = spt.Speedtest()
打印當(dāng)前可用于測試的服務(wù)器列表
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'}]}
獲取當(dāng)前最佳的測試服務(wù)器
spd.get_best_server() print('測試開始,請稍等...')
獲得當(dāng)前的下載速度
download = int(spd.download() / 1024 / 1024)
獲得當(dāng)前的上傳速度
upload = int(spd.upload() / 1024 / 1024) print(f'當(dāng)前下載速度為:{str(download)} MB/s') print(f'當(dāng)前上傳速度為:{str(upload)} MB/s') print('測試已完成!')
打印出最終的返回結(jié)果
測試開始,請稍等...
當(dāng)前下載速度為:12 MB/s
當(dāng)前上傳速度為:13 MB/s
測試已完成!
到此這篇關(guān)于利用Python實現(xiàn)網(wǎng)絡(luò)測試的示例代碼的文章就介紹到這了,更多相關(guān)Python網(wǎng)絡(luò)測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3.7+anaconda 安裝opencv和dlib的問題及解決方法
這篇文章主要介紹了python3.7+anaconda 安裝opencv和dlib的問題及解決方法,本文圖文并茂給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Python機器學(xué)習(xí)中實現(xiàn)距離和相似性計算詳解
這篇文章主要為大家詳細介紹了Python機器學(xué)習(xí)中實現(xiàn)距離和相似性計算的方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03解決Python pandas plot輸出圖形中顯示中文亂碼問題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python初學(xué)者需要注意的事項小結(jié)(python2與python3)
這篇文章主要介紹了Python初學(xué)者需要注意的事項小結(jié),包括了python2與python3的一些區(qū)別,需要的朋友可以參考下2018-09-09Python中使用socks5設(shè)置全局代理的方法示例
這篇文章主要介紹了Python中使用socks5設(shè)置全局代理的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04