Last active
March 6, 2019 23:28
-
-
Save gauden/774d897ea8e0918aadf41e09b1ee86a8 to your computer and use it in GitHub Desktop.
Check ISP speed and store locally, from Reddit https://www.reddit.com/r/Python/comments/awb9v5/useful_python_tricks/ehmpub4/?context=3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment