Code a simple internet speed tester using python

Intro

Hello, today we are going to build an internet's speed tester with python using speedtest-cli library. let's code !

Code

Installation

pip / easy_install

pip install speedtest-cli

or

easy_install speedtest-cli

 

Github

pip install git+https://github.com/sivel/speedtest-cli.git

or

git clone https://github.com/sivel/speedtest-cli.git
cd speedtest-cli
python setup.py install

Just download (Like the way it used to be)

wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +x speedtest-cli

or

curl -Lo speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +x speedtest-cli

Coding

First importing the module

import speedtest

Then we define a tester

tester=speedtest.Speedtest()

Choosing the best server for the test

print('Searching for the best server ..')
bestServer=tester.get_best_server()
print(f'Selecting {bestServer["host"]} located in {bestServer["country"]},{bestServer["name"]}')

Perform a download speed test

print('Checking download speed ..')
downloadSpeed=tester.download()
print('Done !')

Perform a upload speed test

print('Checking upload speed ..')
uploadSpeed=tester.upload()
print('Done !')

The ping test is already done

ping=tester.results.ping

Showing the results

print('Results :')
print(f'-Download speed : {downloadSpeed/1048576 :.2f} Mbits/s')
print(f'-Upload speed : {uploadSpeed/1048576 :.2f} Mbits/s')
print(f'-Ping : {ping :.2f} ms')

Full code

import speedtest

tester=speedtest.Speedtest()


print('Searching for the best server ..')
bestServer=tester.get_best_server()
print(f'Selecting {bestServer["host"]} located in {bestServer["country"]},{bestServer["name"]}')

print('Checking download speed ..')
downloadSpeed=tester.download()
print('Done !')

print('Checking upload speed ..')
uploadSpeed=tester.upload()
print('Done !')

ping=tester.results.ping

print('Results :')
print(f'-Download speed : {downloadSpeed/1048576 :.2f} Mbits/s')
print(f'-Upload speed : {uploadSpeed/1048576 :.2f} Mbits/s')
print(f'-Ping : {ping :.2f} ms')

This is a basic code you can do modifications 

happy coding !

Code a simple internet speed tester using python

You may like these posts