Said Py
Dec. 26, 2021, 11:22 p.m.

Python: How to Test Internet Speed

Probably, you have used the speedtest website before, but did you know that speedtest has a python library.
In this tutorial, We will learn to use the speedtest library to test your internet Speed. We'll also learn how to use speedtest command lines.

Let's get started.


Speedtest instalation

To install speedtest via pip, follow this command:

  1. pip install speedtest-cli

Test internet speed (script)

After installing the speedtest package. Now, let's see how to use it with the code.

In the following code, I'll test my internet download speed.

  1. import speedtest
  2.  
  3. # Speed test
  4. st = speedtest.Speedtest()
  5.  
  6. # Download Speed
  7. ds = st.download()
  8.  
  9. print(ds)
  10.  

let me explain.
First, we import the speedtest package. Then, called Speedtest() class. Next, test my download speed using the download() method. Finally, print the result.

Output:

  1. 3422459.073187817

As you can see, the internet speed is in Bytes . To make it readable, we'll use the following function.

  1. def humansize(nbytes):
  2. suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
  3. i = 0
  4. while nbytes >= 1024 and i < len(suffixes)-1:
  5. nbytes /= 1024.
  6. i += 1
  7. f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
  8. return '%s %s' % (f, suffixes[i])
  9.  
  10. #Readable
  11. print(humansize(ds))
  12.  
  13.  

Output:

  1. 3.56 MB

Now, let's test our upload speed using the upload() method.

  1. import speedtest
  2.  
  3. # Speed test
  4. st = speedtest.Speedtest()
  5.  
  6. # Upload speed
  7. us = st.upload()
  8.  
  9. print(us)
  10.  
  11. #Readable
  12. print(humansize(us))
  13.  

Output:

  1. 386382.6586620888
  2. 301.51 KB

Speedtest command lines

Speedtest also provides the command lines for testing our internet speed.

Usage:

Help command:

  1. speedtest-cli -h

Output:

  1. usage: speedtest-cli [-h] [--no-download] [--no-upload] [--single] [--bytes]
  2. [--share] [--simple] [--csv]
  3. [--csv-delimiter CSV_DELIMITER] [--csv-header] [--json]
  4. [--list] [--server SERVER] [--exclude EXCLUDE]
  5. [--mini MINI] [--source SOURCE] [--timeout TIMEOUT]
  6. [--secure] [--no-pre-allocate] [--version]
  7.  
  8. Command line interface for testing internet bandwidth using speedtest.net.

Test Internet Speed:

  1. speedtest-cli

Output:

  1. Retrieving speedtest.net configuration...
  2. Testing from xxx Telecom (196.89.30.99)...
  3. Retrieving speedtest.net server list...
  4. Selecting best server based on ping...
  5. Hosted by xxx Telecom (xxx) [394.57 km]: 29.1 ms
  6. Testing download speed................................................................................
  7. Download: 8.30 Mbit/s
  8. Testing upload speed......................................................................................................
  9. Upload: 10.33 Mbit/s

This command above tests ping , download speed, and upload speed.

Test Internet Speed: with the share link:

  1. speedtest-cli --share

Output:

  1. Retrieving speedtest.net configuration...
  2. Testing from xxx Telecom (196.89.30.99)...
  3. Retrieving speedtest.net server list...
  4. Selecting best server based on ping...
  5. Hosted by xxx Telecom (xxx) [394.57 km]: 28.131 ms
  6. Testing download speed................................................................................
  7. Download: 3.74 Mbit/s
  8. Testing upload speed......................................................................................................
  9. Upload: 0.39 Mbit/s
  10. Share results: http://www.speedtest.net/result/12339819892.png

As you can see, we've got a URL of the results. Let's open it in the browser.

Result:

Speed Test Result

We hope this is easy to understand. See you later.


References:

https://pypi.org/project/speedtest-cli/
speedtest example (PyOnlyCode)
Speedtest-cli Example (PyOnlyCode)

By article asked0question(s)

1

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup