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