Here, we learn how to detect the connected network speed of the computer using Python. To do that, we can use the speedtest-cli
library, which has the required method for detecting speed.
pip install speedtest-cli
The speedtest
module contains:
download
– method to test the download speed.upload
– method to test the upload speed of the selected network.# import speedtest moduleimport speedtestspeed_test = speedtest.Speedtest()download_speed = speed_test.download()print("Your Download speed is", download_speed)upload_speed = speed_test.upload()print("Your Upload speed is", upload_speed)
The above code prints the upload/download speed in bytes. Let’s create a method to convert bytes to MB:
def bytes_to_mb(bytes):KB = 1024 # One Kilobyte is 1024 bytesMB = KB * 1024 # One MB is 1024 KBreturn int(bytes/MB)val = bytes_to_mb(1024 * 1024)print(val)
Here, we have used the above function on our code:
import speedtestspeed_test = speedtest.Speedtest()def bytes_to_mb(bytes):KB = 1024 # One Kilobyte is 1024 bytesMB = KB * 1024 # One MB is 1024 KBreturn int(bytes/MB)download_speed = bytes_to_mb(speed_test.download())print("Your Download speed is", download_speed, "MB")