Created
November 8, 2017 23:10
-
-
Save nijave/1a908511065ed2a71f40ad620e4a6efb to your computer and use it in GitHub Desktop.
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 requests | |
from bs4 import BeautifulSoup | |
import pandas as pd | |
import pickle | |
import time | |
import signal | |
import sys | |
def get_status(): | |
print("\t- Loading status page") | |
#Get modem status page | |
status_page = requests.get('http://192.168.100.1/cgi-bin/status_cgi') | |
soup = BeautifulSoup(status_page.content, 'lxml') | |
print("\t- Extracting tables") | |
#Extract tables | |
tables = soup.find_all('table') | |
down = str(tables[1]) | |
up = str(tables[3]) | |
status = str(tables[5]) | |
print("\t- Creating dataframes") | |
#Read into dataframes | |
df_down = pd.read_html(down, header=0)[0] | |
df_up = pd.read_html(up, header=0)[0] | |
df_status = pd.read_html(status)[0] | |
#Special handing for the status info df (convert to dict) | |
dict_status = {} | |
for k,v in df_status.iterrows(): | |
row = v.iteritems() | |
dict_status[next(row)[1]] = next(row)[1].replace(':','') | |
dict_status = {v:k for k,v in dict_status.items()} | |
data = { | |
'down': df_down, | |
'up': df_up, | |
'status': dict_status | |
} | |
return data | |
def write_status(data): | |
print("\t- Saving data to file") | |
output = open('modem_stats.dat', 'ab') | |
pickle.dump(data, output) | |
output.close() | |
def signal_handler(signal, frame): | |
global running | |
print("Stopping!") | |
running = False | |
def update(): | |
write_status(get_status()) | |
signal.signal(signal.SIGINT, signal_handler) | |
running = True | |
while running: | |
print(str(time.time()) + ": Running") | |
update() | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment