Let’s build an Internet Performance and Network Speed Tester
As internet users, we usually rely on fast.com and speedtest.net for all of our internet performance and network speed tests. Although this is not bad in terms of utilities, it would be great if we could integrate these tools in our own applications to run all these tests inside the core of our project, without the need of going back and forth in between our app and these tools, wouldn’t be great if we could do so?
Well, we certainly can and we will focus on speedtest.net to give you guys an example on how to proceed with this in this article, are you ready to start this journey, buddy?
We will need three modules, or packages, or libraries or whatever you want to call it in order for us to run this project. We can install them using pip or downloading the files. Most of these packages have their own descriptive section in the the Python Package Index(PyPI)repository. Let’s add a summary of some of these:
SPEEDTEST | https://pypi.org/project/speedtest/
Description | Speed test module for Python
Version | speedtest==0.0.1
Command | pip install speedtest
Requires | Python >=3.7
COLORAMA | https://pypi.org/project/colorama/
Description | Cross-platform colored terminal text.
Version | colorama==0.4.4
PIP Command | pip install colorama
Requires | Python >=2.7, !=3.0.*,
TQDM | https://pypi.org/project/tqdm/
Description | Fast, Extensible Progress Meter
Version | tqdm==4.62.3
PIP Command | pip install speedtest
Requires | Python !=3.0.*, !=3.1.* to !=3.3.*, >=2.7
Now that we have all these modules installed, let’s create a speed_test_pjt.py file and start our first lines of code by adding the necessary imports in this file as follows:
Next, we need to initialize our colorama module, passing the autoreset parameter as True, that way, the console colors will automatically reset to default after the last colorama command has been invoked. I am pretty sure you will love this amazing package, buddy!
Once we have added this, we proceed to create an instance of the Speed Test class using the SpeedTest() function, get the most optimal server available for our test using get_best_server() in our instance, calling the download() and upload() functions to get downloading and uploading speed information, and save all this data in a dictionary called res_dict that will contain all the returning results of these functions.
We will use tqdm() method of our installed progress bar to display in the screen a fake progress of the speed test events so that way something is being shown in the screen while these events are taking place. Let’s see the code below, young coders:
Next, let’s retrieve the information from res_dict and assign to variables with a specific format. This will come handy is terms of displaying more readable data:
Last but not least, let’s give that nice looking table effect to our results combining the power of colorama, along with some lines, used as dividers, to make it look smooth, shall we?
Ready, tigers!! We can now say that our code has been completed: Congrats! You guys did a great job!
Final Code for this Project
Well, we’ve come to an end. If you found this article helpful, please click on the clap 👏 button below as much as you like. You can also follow me on Medium and if you have any doubts, concerns or simply wanna say something, feel free to comment! I’d be happy to read your feedbacks! 🙂