Easy Python script for monitoring internet speed

James Leynse/GettyImages
facebooktwitterreddit

When you sign up with your internet provider, you agree to receive a specific speed for your internet. However, the exact speed can vary significantly from moment to moment based on a number of factors. Understanding and tracking your internet speed can help troubleshoot issues and ensure you’re getting the bandwidth you’re paying for. It can also help you find the best times to do bandwidth-intensive tasks, like downloading a large app or game. With Python, you can monitor your network speed easily.

Checking Your Internet Speed

Download Speed

Download speed is the rate at which data transfers from the internet to your device in Mbps (Megabits per second). It is the metric that determines how fast you can surf the internet, stream movies, and download files.

Upload Speed

Upload speed measures how fast data goes from your device to the internet in Mbps. This metric will be more important for people who are content creators and need to upload a lot of videos and other files. A faster upload speed will make uploading a lot faster.

Ping/Latency

Measured in milliseconds (ms), ping is the time it takes for a small data packet to travel to a server and back. Lower ping times are better, especially for real-time applications like online gaming or video conferencing.

How Speed Tests Work

Download Test

The test client downloads small chunks of data from a server, gradually increasing the size of these chunks. The total size of data downloaded over a fixed time interval gives the download speed.

Upload Test

Similarly, the test client uploads data to the server, measuring how much can be transferred in a given time to determine the upload speed. 

Latency/Ping Test

A small data packet travels from the user’s device to the server and back. The time taken for this round trip determines the latency.

Using Python to check the internet speed

Python’s speedtest-cli library connects directly to Speedtest.net, making it easy to run these tests with a simple script. It selects the nearest server to minimize latency and ensure accurate results. It also reflects real-time network conditions, such as congestion or routing inefficiencies, which can affect your results. If you need help getting started with Python, check out our full list of Python projects.

Why monitoring your network speed is important

  • Monitoring your internet speed helps verify if you’re getting the speeds promised by your Internet Service Provider (ISP).
  • Regular speed checks can help you detect network problems such as throttling, which occurs when your ISP intentionally slows down your connection based on usage patterns.

The Python script

Install the library

The script we are going to use requires installing the speedtest-cli library first. Do so by typing the following command on the command prompt:

pip install speedtest-cli

Check your network speed

import speedtest

def check_speed():
st = speedtest.Speedtest()
download_speed = st.download() / 1_048_576 # Convert to Mbps
upload_speed = st.upload() / 1_048_576 # Convert to Mbps
ping = st.results.ping
print(f"Download speed: {download_speed:.2f} Mbps")
print(f"Upload speed: {upload_speed:.2f} Mbps")
print(f"Ping: {ping} ms")

check_speed()

# Wait for user to press Enter before closing
input("Press Enter to close the script...")

Conclusion

Running the script will show you your current download, upload, and ping times. Storing the results in a spreadsheet or database or writing an additional script to do it for you can help you keep track of your speeds over time and find the best times to perform data-intensive tasks, such as uploading or downloading.

Follow GeekSided for more tips on monitoring your network using Python.

feed