Skip to content

Instantly share code, notes, and snippets.

@colebob9
Last active January 26, 2018 16:30
Show Gist options
  • Save colebob9/f435292afb097a08911386e54f101bb3 to your computer and use it in GitHub Desktop.
Save colebob9/f435292afb097a08911386e54f101bb3 to your computer and use it in GitHub Desktop.
For automatically testing and logging internet speeds
# SpeedTester v2.1
# colebob9
# Python 3
# (rewrite)
import speedtest
import os
import csv
import time
first_time = False
# Create and set up csv file
if not os.path.isfile('tests.csv'):
outputFile = open('tests.csv', 'w', newline='')
outputWriter = csv.writer(outputFile)
first_time = True
print("Made new CSV file.")
else:
outputFile = open('tests.csv', 'a')
outputWriter = csv.writer(outputFile)
servers = []
s = speedtest.Speedtest()
print("Finding servers...")
s.get_servers(servers)
# add better timestamp for excel
currentDate = time.strftime("%Y-%m-%d %H:%M:%S")
# Start elasped time for test
start_test = time.time()
print("Finding best server...")
best_server = s.get_best_server()
print("Found best server: {}".format(best_server['host']))
print("Testing download...")
down_result = s.download()
print("Download (in bits): {}".format(down_result))
print("Testing upload...")
up_result = s.upload(pre_allocate=False)
print("Upload (in bits): {}".format(up_result))
share_png = s.results.share()
print("Results Share: {}".format(share_png))
results_dict = s.results.dict()
end_test = time.time()
elasped_time_test = (end_test - start_test)
print("Test took " + str(round(elasped_time_test , 1)) + " seconds")
# First time, write titles at top of csv
if first_time:
titles = []
titles.append("timestamp")
titles.append("timetaken")
for key in results_dict.keys():
if key == "server":
continue
if key == "timestamp":
continue
titles.append(key)
titles.append('')
for key in best_server:
titles.append(key)
outputWriter.writerow(titles)
outputWriter.writerow([''])
# organize results to csv on one line
# organize values
values = []
values.append(currentDate) # write timestamp first
values.append(elasped_time_test)
for key, value in results_dict.items():
if key == "server":
continue
if key == "timestamp":
continue
values.append(value)
values.append('')
# add server stats to csv, not in a dictionary
for key, value in best_server.items():
values.append(value)
# write to csv
print('')
print("All values:")
print(values)
outputWriter.writerow(values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment