Python 100 project #9: Speedtest.net

As my daily job as a network provider, I need to ask the customer to check their internet speed by visiting speedtest.net. I used the third party library to test the speedtest.net.

 

Output Example:

100p $ python3 internet_speedtest.py 
the closest speedtest.net server result is as follows
download: 25.30280799925929Mbps
upload: 4.161998302377309Mbps
latency: 4.556ms

 

Here is the code:

import speedtest

def get_result(id=None):
    servers = []
    if id:
        servers.append(id)

    s = speedtest.Speedtest()
    s.get_servers(servers)
    s.get_best_server()
    s.download()
    s.upload()

    results_dict = s.results.dict()

    return results_dict['download'], results_dict['upload'], results_dict['ping']


if __name__ == '__main__':

    download, upload, latency = get_result()

    print(f"the closest speedtest.net server result is as follows\n"
          f"download: {download/1000**2}Mbps\n"
          f"upload: {upload/1000**2}Mbps\n"
          f"latency: {latency}ms\n")