1、背景
当你的计算机进行冲浪非常卡顿的时候,或许你很想知道当前你连接互联网的冲浪网速是多少。
可以通过Speedtest官网直接测试网络速度。
网址: https://www.speedtest.net/
注意每次测试的结果都不一样,因为存在网络的波动,以上仅是一个参考值!
同时这个speedtest有一个开源的python三方工具包:
https://github.com/sivel/speedtest-cli
可以直接用pip安装:pip install speedtest-cli
(jn10010537) C:\Users\Administrator>pip install speedtest_cli
Collecting speedtest_cli
Downloading speedtest_cli-2.1.3-py2.py3-none-any.whl (23 kB)
Installing collected packages: speedtest_cli
Successfully installed speedtest_cli-2.1.3
(jn10010537) C:\Users\Administrator>speedtest-cli -h
usage: speedtest-cli [-h] [--no-download] [--no-upload] [--single] [--bytes] [--share] [--simple] [--csv]
[--csv-delimiter CSV_DELIMITER] [--csv-header] [--json] [--list] [--server SERVER]
[--exclude EXCLUDE] [--mini MINI] [--source SOURCE] [--timeout TIMEOUT] [--secure]
[--no-pre-allocate] [--version]
Command line interface for testing internet bandwidth using speedtest.net.
-------------------------------------------------------------------------- https://github.com/sivel/speedtest-cli
optional arguments:
-h, --help show this help message and exit
--no-download Do not perform download test
--no-upload Do not perform upload test
--single Only use a single connection instead of multiple. This simulates a typical file transfer.
--bytes Display values in bytes instead of bits. Does not affect the image generated by --share, nor
output from --json or --csv
--share Generate and provide a URL to the speedtest.net share results image, not displayed with --csv
--simple Suppress verbose output, only show basic information
--csv Suppress verbose output, only show basic information in CSV format. Speeds listed in bit/s and
not affected by --bytes
--csv-delimiter CSV_DELIMITER
Single character delimiter to use in CSV output. Default ","
--csv-header Print CSV headers
--json Suppress verbose output, only show basic information in JSON format. Speeds listed in bit/s
and not affected by --bytes
--list Display a list of speedtest.net servers sorted by distance
--server SERVER Specify a server ID to test against. Can be supplied multiple times
--exclude EXCLUDE Exclude a server from selection. Can be supplied multiple times
--mini MINI URL of the Speedtest Mini server
--source SOURCE Source IP address to bind to
--timeout TIMEOUT HTTP timeout in seconds. Default 10
--secure Use HTTPS instead of HTTP when communicating with speedtest.net operated servers
--no-pre-allocate Do not pre allocate upload data. Pre allocation is enabled by default to improve upload
performance. To support systems with insufficient memory, use this option to avoid a
MemoryError
--version Show the version number and exit
(jn10010537) C:\Users\Administrator>
2、使用示例
netspeed.py脚本内容如下:
# netspeed.py
# jn10010537
# pip install speedtest-cli
import speedtest
# 创建一个新的速度测试对象
test = speedtest.Speedtest()
# 获取可用于测试的服务器列表
test.get_servers()
# 筛选出最佳服务器
best = test.get_best_server()
while 1:
# 下载速度
download_speed = round(test.download() / 1024 / 1024,1)
# 上传速度
upload_speed = round(test.upload() / 1024 / 1024,1)
# 输出结果
print()
print(f"Your ping is: {test.results.ping} 毫秒!")
print(f"下载速度:{download_speed}Mbit/s")
print(f"上传速度:{upload_speed}Mbit/s")
注意运行时间比较长,程序终端打印如下: