You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python程序中调用speedtest.py时使用安全服务器

在Python中使用speedtest.py指定安全服务器

Speedtest要求使用安全服务器才能正常测速,命令行的--secure参数对应Python库中Speedtest类初始化时的关键字参数secure=True,以下是正确的实现代码:

import speedtest

# 初始化Speedtest对象时指定使用安全服务器
s = speedtest.Speedtest(secure=True)
servers = s.get_best_server()

# 执行下载测试
download_result = s.download()
# 执行上传测试
upload_result = s.upload()

# 格式化输出测速结果(转换为Mbps)
print(f"下载速度: {download_result / 10**6:.2f} Mbps")
print(f"上传速度: {upload_result / 10**6:.2f} Mbps")

你之前尝试的两种写法都不符合库的API规范:

  • speedtest.Speedtest("secure") 错误,因为Speedtest类初始化不接受字符串类型的"secure"参数,需传入关键字参数secure=True
  • s.get_best_server("secure") 错误,get_best_server方法不支持该参数,安全服务器的配置需在初始化对象时完成

内容的提问来源于stack exchange,提问作者Shawn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:35:41