使用python的“speedtest-cli”向特定服务器发出请求

pinkon5k  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(340)

我写了一段代码,每3小时用Python做一次速度测试。get_best_server()可以向远离我的服务器发出请求。当我在网站上做的时候,它真的找到了最近的城市。我有最接近我的服务器ID,我如何请求它?

import speedtest

    test = speedtest.Speedtest(secure=1)
    test.get_servers()
    best = test.get_best_server()
    
    print(f"Found:{best['host']} located in  {best['country']}")

    download_result = test.download()
    upload_result = test.upload()
    ping_result = test.results.ping

    print(f"Download Speed: {download_result / 1024 / 1024:.2f} Mbit/s")
    print(f"Upload Speed: {upload_result / 1024 / 1024:.2f} Mbit/s")
    print(f"Ping: {ping_result}s")
cqoc49vn

cqoc49vn1#

迟来的答案,却让我面临同样的问题;下面是我如何解决它(官方示例):

import speedtest

test = speedtest.Speedtest(secure=True)

#define the list of servers that you want to run against
test.get_servers(servers=["5738","10241"])

best = test.get_best_server()
print(f"Found:{best['host']} located in  {best['country']}")

download_result = test.download()
print(f"Download Speed: {download_result / 1024 / 1024:.2f} Mbit/s")

upload_result = test.upload()
print(f"Upload Speed: {upload_result / 1024 / 1024:.2f} Mbit/s")

ping_result = test.results.ping
print(f"Ping: {ping_result}s")

但是,如果您的server-list包含没有HTTPS的服务器,则会添加secure=True,因此会出现错误。因此,要么删除secure=True,要么只包含HTTPS服务器。

相关问题