a shot of dev knowledge

Related tags

How to detect internet speed using Python

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 .

Install speedtest library

pip install speedtest-cli

Implementing the speed test

The speedtest module contains:

  • download – method to test the download speed.
  • upload – method to test the upload speed of the selected network.
# import speedtest module 
import speedtest

speed_test = speedtest.Speedtest()

download_speed = st.download()
print("Your Download speed is", download_speed) 

upload_speed = st.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 bytes
  MB = KB * 1024 # One MB is 1024 KB
  return int(bytes/MB)

val = bytes_to_mb(1024 * 1024)

Here, we have used the above function on our code:

download_speed = bytes_to_mb(st.download())

print("Your Download speed is", download_speed) 

Related tags

View all Courses
Related Courses
Related Courses
View all Courses

Keep Exploring