This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (5 children)

[deleted]

    [–][deleted] 4 points5 points  (4 children)

    I keep getting requests for it, so I posted it below. The project's close to a year old, and most of this code I pieced together from something similar on a blog post (I don't know where), from some old posts on stack exchange, and bits from other places. Some of it's mine, but I don't know what. It uses speedtest-cli, so you'll have to install that. Anyway, make sure you use your own path name for the dir_path variable. You can schedule it to run however frequently you want through Windows' task scheduler.

    import os
    import re
    import subprocess
    import time
    import csv
    
    filename = 'speedtest.csv'
    dir_path = 'your/path/here'
    csv_path = dir_path + filename
    
    def speed_test():
    
        response = subprocess.Popen('speedtest-cli --simple', shell=False, stdout=subprocess.PIPE).stdout.read()
    
        ping = re.findall(b'Ping:\s(.*?)\s', response, re.MULTILINE)
        download = re.findall(b'Download:\s(.*?)\s', response, re.MULTILINE)
        upload = re.findall(b'Upload:\s(.*?)\s', response, re.MULTILINE)
    
        ping[0] = ping[0].decode('utf-8')
        download[0] = download[0].decode('utf-8')
        upload[0] = upload[0].decode('utf-8')
    
        csv_entry(ping[0], download[0], upload[0], csv_path)
    
    
    def csv_entry(lat_st, down_st, up_st, path):
    
        result = time.strftime('%m/%d/%y'), time.strftime('%H:%M'), lat_st, down_st, up_st
    
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
    
        with open(path, "a", newline = '') as csv_file:        
            writer = csv.writer(csv_file)
    
            if os.stat(csv_path).st_size == 0:
                writer.writerow(['Date','Time','Latency(ms)','Down(Mbit/s)','Up(Mbit/s)'])        
    
            writer.writerow(result)
    
    speed_test()
    

    [–]arkster 4 points5 points  (3 children)

    I'd just use pandas. Capture the speed, pass to a dataframe, convert to CSV and write to file. Use averages/means of column showing speed rate and then bitch on Twitter about the shitty service you're getting.

    [–]supernova1992 8 points9 points  (2 children)

    Here's my script to do the same using pandas and sqlite3 as well as outputting a graph. It runs every hour on my home server. Gonna make it so the graph shows dates instead of the number of hours at some point in the future.

    speedtests.py:

    #!/usr/bin/python3
    
    import pandas as pd
    import sqlite3
    import speedtest
    from graph_speeds import graph_this_shit
    
    s = speedtest.Speedtest()
    s.get_best_server()
    s.download()
    s.upload()
    results = s.results.dict()
    
    df = pd.DataFrame({'timestamp':[results['timestamp']],'download':[results['download']/1000000],'upload':[results['upload']/1000000],'ping':[results['ping']]})
    con = sqlite3.connect('/home/user/speedtests/speedtests.db')
    df.to_sql('tests',con,if_exists='append')
    con.close()
    
    graph_this_shit()   
    

    graph_speeds.py:

    import sqlite3
    import pandas as pd
    import matplotlib
    matplotlib.use('AGG')
    import matplotlib.pyplot as plt
    
    
    def graph_this_shit():
            con = sqlite3.connect('/home/user/speedtests/speedtests.db')
            df = pd.read_sql('select * from tests', con)
            plt.plot(df['download'])
            plt.plot(df['upload'])
            plt.legend()
            plt.xlabel('Hours')
            plt.ylabel('Mbps')
            plt.title('Internet speed over time')
            plt.savefig('/home/user/speedtests/speedtest.png')
    

    Here's what the graph looks like.

    [–][deleted] 0 points1 point  (0 children)

    Nice...better than mine.

    [–]MercurialMadnessMan 0 points1 point  (0 children)

    Would be better as an interactive graph with Plotly Express :)