Internet Speed Test in Python

Creating an Internet Speed Test in Python

Internet speed tests are essential tools for users to check the quality and performance of their internet connections. In this article, we will walk you through how to create a simple internet speed test using Python and the ‘speedtest-cli’ library. This tool will allow you to measure your download and upload speeds.

Prerequisites

Before you begin, make sure you have the following:

  1. Python installed on your system (you can download it from python.org).
  2. The ‘speedtest-cli’ library, which can be installed using pip:
pip install speedtest-cli

Writing the Python Script

Now, let’s create a Python script that uses the ‘speedtest-cli’ library to measure internet speed. We will also return the results in JSON format.

import speedtest
import json

def test_internet_speed():
    st = speedtest.Speedtest()
    st.get_best_server()

    download_speed = st.download() / 10**6  # Convert to Mbps
    upload_speed = st.upload() / 10**6  # Convert to Mbps

    results = {
        "download_speed": download_speed,
        "upload_speed": upload_speed
    }

    return results

if __name__ == "__main__":
    speed_results = test_internet_speed()
    json_results = json.dumps(speed_results, indent=4)
    print(json_results)

In this script:

  • We import the ‘speedtest’ and ‘json’ libraries.
  • The test_internet_speed function uses the ‘speedtest-cli’ library to measure download and upload speeds and returns the results as a dictionary.
  • The results are then converted to JSON format using json.dumps.

Running the Speed Test

You can run the script by executing it in your terminal or command prompt. Ensure you are in the directory where the script is located. To run the script:

python speedtest.py

You will see the internet speed test results in JSON format printed in your terminal.

Using the Results

Now that you have your internet speed test results in JSON format, you can use them in various ways, such as creating a web application to display the results or integrating them into your network monitoring system.

Here’s an example of how to integrate the results into a simple web application using Flask:

  1. Create a Flask application as described in a previous response, and modify the Flask route to return the JSON results:
from flask import Flask, jsonify
import speedtest

app = Flask(__name)

@app.route('/speedtest')
def speed_test():
    speed_results = test_internet_speed()  # Use the test_internet_speed function from the script
    return jsonify(speed_results)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8989, debug=True)
  1. Access the ‘/speedtest’ endpoint of your Flask application in a web browser to see the JSON results of the speed test.

You’ve now created a simple internet speed test using Python, measured your download and upload speeds, and can integrate the results into various applications or services to monitor your internet connection.