How to Test Internet Speed in Python
Tags: python
Published: 2021-12-06 20:56:59

Probably, you have used the [speedtest](https://www.speedtest.net/) 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: 
```bash 
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. 
```python 
import speedtest
 
# Speed test
st = speedtest.Speedtest()
 
# Download Speed
ds = st.download()
 
print(ds)
```
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:
```bash
3422459.073187817 
```
As you can see, the internet speed is in ***Bytes***. To make it readable, we'll use the following function.
```python
def humansize(nbytes):
    suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
    i = 0
    while nbytes >= 1024 and i < len(suffixes)-1:
        nbytes /= 1024.
        i += 1
    f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
    return '%s %s' % (f, suffixes[i])
 
#Readable
print(humansize(ds))
```
Output: 
```bash
3.56 MB
```

Now, let's test the upload speed using the ***upload()*** method.
```python
import speedtest
 
# Speed test
st = speedtest.Speedtest()
 
# Upload speed
us = st.upload()
 
print(us)
 
#Readable
print(humansize(us))
```
Output:
```bash 
386382.6586620888
301.51 KB
```

## Speedtest command lines
Speedtest also provides the command lines for testing our internet speed.
### Usage:
Help command:
```bash 
speedtest-cli -h
```

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

Test Internet Speed:
```bash 
speedtest-cli
```
Output:
```bash 
    Retrieving speedtest.net configuration...
    Testing from xxx Telecom (196.89.30.99)...
    Retrieving speedtest.net server list...
    Selecting best server based on ping...
    Hosted by xxx Telecom (xxx) [394.57 km]: 29.1 ms
    Testing download speed................................................................................
    Download: 8.30 Mbit/s
    Testing upload speed......................................................................................................
    Upload: 10.33 Mbit/s
```
This command above tests ***ping***, ***download***, and ***upload*** speed. 

Test Internet Speed with the share link: 
```bash 
speedtest-cli --share
```

Output: 
```bash 
    Retrieving speedtest.net configuration...
    Testing from xxx Telecom (196.89.30.99)...
    Retrieving speedtest.net server list...
    Selecting best server based on ping...
    Hosted by xxx Telecom (xxx) [394.57 km]: 28.131 ms
    Testing download speed................................................................................
    Download: 3.74 Mbit/s
    Testing upload speed......................................................................................................
    Upload: 0.39 Mbit/s
    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:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27fzjvztrll68hx7jzaa.png)

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

## References
[https://pypi.org/project/speedtest-cli/](https://pypi.org/project/speedtest-cli/)

[Python: Test Internet Speed](https://pytutorial.com/Python-test-internet-speed)