Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to detect internet speed using Python

Programming Bytes

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

val = bytes_to_mb(1024 * 1024)
print(val)

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

import speedtest

speed_test = speedtest.Speedtest()

def bytes_to_mb(bytes):
  KB = 1024 # One Kilobyte is 1024 bytes
  MB = KB * 1024 # One MB is 1024 KB
  return int(bytes/MB)

download_speed = bytes_to_mb(speed_test.download())
print("Your Download speed is", download_speed, "MB") 

RELATED TAGS

python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring