0

I wrote a code that does a speed test every 3 hours using python. get_best_server() can make requests to servers far away from me. When I do it on the website, it really finds the closest city-based. I have the server id closest to me, how can I request it?

    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")

Niyaz
  • 797
  • 1
  • 8
  • 18
Hamsi
  • 1
  • 1

1 Answers1

0

Late answer, but I face the same issue; here's how I solved it (official example) :

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")

However, since you add secure=True if your server-list contains servers without HTTPS, it gives you an errors. Because of that, either remove secure=True or only include servers with HTTPS.

Niyaz
  • 797
  • 1
  • 8
  • 18