利用Python實現(xiàn)網(wǎng)絡測試的示例代碼
Speedtest CLI 專為軟件開發(fā)人員、系統(tǒng)管理員和計算機愛好者等打造,是 Ookla® 提供技術支持的首款正式 Linux 本機 Speedtest 應用程序。
Speedtest CLI是使用python語言開發(fā)的,不僅可以直接在命令行運行。也可以作為python模塊在python IDE中直接調用。
首先,看一下如何在python應用中進行調用,使用pip直接安裝。
pip install speedtest-cli
將該模塊直接導入到我們當前的代碼塊中。
import speedtest as spt
創(chuàng)建網(wǎng)絡測試對象
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實現(xiàn)網(wǎng)絡測試的示例代碼的文章就介紹到這了,更多相關Python網(wǎng)絡測試內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python3.7+anaconda 安裝opencv和dlib的問題及解決方法
這篇文章主要介紹了python3.7+anaconda 安裝opencv和dlib的問題及解決方法,本文圖文并茂給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
解決Python pandas plot輸出圖形中顯示中文亂碼問題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python初學者需要注意的事項小結(python2與python3)
這篇文章主要介紹了Python初學者需要注意的事項小結,包括了python2與python3的一些區(qū)別,需要的朋友可以參考下2018-09-09

