speedtest py

Solutions on MaxInterview for speedtest py by the best coders in the world

showing results for - "speedtest py"
Silvia
30 Jan 2020
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        finalspeeds = speeds/count
22    return finalspeeds
23
24#menu loop
25while True:
26    repeat = input('1, 2, 3 or press {ENTER} to quit\n>>>')
27    if repeat == '1':
28        #single iteration
29        x = get_final_speed()
30        print(f'done, your download speed is {x}mb/s')
31    elif repeat == '2':
32        #2 iterations and finds the average speed
33        x = looped_av(2)
34        print(f'done, your average download speed is {x}mb/s')
35    elif repeat == '3':
36        #finds out how accurate the user wants the average to be, pretty pointless i know
37        times_through = int(input('how many times do you want the test to run?\n>>>'))
38        #iterates and finds the average download speed
39        x = looped_av(times_through)
40        print(f'done, your average download speed is {x}mb/s')  
41    else:
42        #breaks from the loop
43        break