Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

About SpeedTest

Speedtest is the world’s most popular network speed testing platform. Python has a corresponding library called SpeedTest – CLI. We can use the client for network testing, or we can use the class object for code testing. Pip3 install speedtest- CLI can be installed.

Network speed test tool

code

from speedtest_cli import Speedtest

Instantiate the test class
spt = Speedtest()

Get the fastest server
spt.get_best_server()

print('=============== Start testing download speed ================')

Test the download speed in bytes
downSp = spt.download()

Print the result and convert it to Mb
print(F '=============== Download speed is:{downSp /1024/1024:2.f} Mb/s ================')

print('=============== Start test upload speed ================')

# Test upload speed in bytes
upSp = spt.upload()

print(F '=============== Upload speed is:{upSp /1024/1024:2.f} Mb/s ================')
Copy the code

Code – v2

We thought about making a GUI test widget.

# -*- coding: utf-8 -*-
import PySimpleGUI as sg
from speedtest_cli import Speedtest
import threading

# instantiate the speed object
spt = Speedtest()

# Set the theme
sg.theme('Black')

# Layout elements
one_line = [sg.Text('Best Server:'),sg.Text(size=(50.1),key='-BSTS-')]
two_line = [sg.Text('Download Speed:'),sg.Text(size=(50.1),key='-DLS-')]
three_line = [sg.Text('Upload speed:'),sg.Text(size=(50.1),key='-UPS-')]
four_line = [sg.Button('Start testing'),sg.Button('exit')]
five_line = [sg.Text(size=(50.1),key='-INFO-')]

# layout
layout = [
    one_line,
    two_line,
    three_line,
    four_line,
    five_line
]


Create window
window = sg.Window('Speed Test Kit', layout)

# Event loop
while True:

    event, values = window.read(timeout=100)
    if event in (sg.WIN_CLOSED, 'exit') :# The user closes the window or clicks exit to exit
        break

    if event == 'Start testing':

        # Following the code below, the form will not respond to the problem
        window['-INFO-'].update('Start getting servers... ',text_color='red')
        res = spt.best.get('sponsor')
        print(res)
        window['-BSTS-'].update(res,text_color='yellow')

        window['-INFO-'].update('Start getting tested download rates... ',text_color='red')
        dSp1=spt.download()
        dSp=f'{dSp1/1024/1024:2.f} Mb/s'
        window['-DLS-'].update(dSp,text_color='yellow')

        window['-INFO-'].update('Start getting test upload rate... ',text_color='red')
        uSp1=spt.upload()
        uSp=f'{uSp1/1024/1024:2.f} Mb/s'
        window['-UPS-'].update(uSp,text_color='yellow')

        window['-INFO-'].update('Tests completed... ',text_color='red')
        sg.popup_ok

        print(f'Event: {event}')
        print(str(values))

window.close()
Copy the code

The code analysis

The above code is divided into two parts. The first part uses speedtest- CLI to measure speed. The second part uses PySimpleGUI to interact with the user. In the GUI event loop, we detect the start of the speed measurement when a “test start” event occurs, and update the GUI text elements to remind the user of the current progress.

The effect

To this, network speed test small tool development is completed, hurriedly try it.

Tip

Sharp-eyed you may have noticed, or have noticed in your own practice, that when we click to start the test, the window goes unresponsive, and the update doesn’t appear in the middle, only showing the test results at the end. Why is that? We’ll find out next time.

That’s all for today, thank you for reading, and we’ll see you next time.