speedtest cli python example

Solutions on MaxInterview for speedtest cli python example by the best coders in the world

showing results for - "speedtest cli python example"
Marlene
24 May 2017
1#pip3 install speedtest-cli
2import speedtest
3
4#function that gets the download speed in mega bytes per second
5def get_final_speed():
6    rawspeed = speedtest.Speedtest().download()
7    roundedspeed = round(rawspeed)
8    finalspeed = roundedspeed / 1e+6
9    return finalspeed
10
11#function that finds the average downloadspeed in mega bytes a second
12def looped_av(y):
13    finalspeeds = 0
14    for i in range(y):
15        x = get_final_speed()
16        speeds = 0
17        count = 0
18        count += 1
19        speeds += x
20        print(f'{i+1}. {x}mb/s')
21        
22
23#menu loop
24while True:
25    repeat = input('1, 2, 3 or press {ENTER} to quit\n>>>')
26    if repeat == '1':
27        #single iteration
28        x = get_final_speed()
29        print(f'done, your download speed is {x}mb/s')
30    elif repeat == '2':
31        #2 iterations and finds the average speed
32        x = looped_av(2)
33        print(f'done, your average download speed is {x}mb/s')
34    elif repeat == '3':
35        #finds out how accurate the user wants the average to be, pretty pointless i know
36        times_through = int(input('how many times do you want the test to run?\n>>>'))
37        #iterates and finds the average download speed
38        x = looped_av(times_through) 
39    else:
40        #breaks from the loop
41        break