Last active
December 20, 2015 09:40
-
-
Save tmeissner/6109895 to your computer and use it in GitHub Desktop.
python cgi script to generate temperature data in JSON format
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/python | |
# -*- coding: utf-8 -*- | |
import cgi | |
import json | |
import re | |
import os | |
import hashlib | |
import base64 | |
import datetime | |
from pbkdf2 import safe_str_cmp, pbkdf2_hex | |
# get cgi object | |
form = cgi.FieldStorage() | |
# example values, change when going online | |
salt = "a683c64de226677703f56e6b6ead94bbc3690ec5293c3de3ffdc932dc54aa44b" | |
password = "3404ca4e934bee674610d350a736bcd8ef02db768765a19e45e53f2a5c8ce982" | |
userpwd = "" | |
# get pwd, salt it, hash it | |
if (form.getvalue('pwd')): | |
userpwd = pbkdf2_hex(base64.b64decode(form.getvalue('pwd')), salt, 1000, 32, hashlib.sha256) | |
regstring = '\d\d\d\d,\d\d\,\d\d' | |
if (form.getvalue('period')): | |
period = form.getvalue('period') | |
if (period == "0"): | |
regstring = datetime.datetime.now().strftime('%Y,%m,%d') | |
if (period == "1"): | |
regstring = datetime.datetime.now().strftime('%Y,%m,') + '\d\d' | |
# pwd correct | |
if (safe_str_cmp(userpwd, password)): | |
# open temp log txt file & find requested temp data | |
f = open("/home/pi/projects/temp.log", "rb") | |
if (period == "3"): | |
f.seek(-48, 2) | |
# find date & temp strings | |
datetemps = re.findall(r'' + regstring + '\s+\d\d\,\d\d\,\d\d\s+\d\d\.\d\d\d\s+\d\d\.\d\d\d', f.read()) | |
f.close() | |
# generate temp strings | |
dates = [date[:20] for date in datetemps] | |
room = [float(temp[25:30]) for temp in datetemps] | |
cpu = [float(temp[-6:-1]) for temp in datetemps] | |
# put date & temp strings into json object | |
data = json.dumps({'pwdvalid': 1,'time': dates, 'room': room, 'cpu': cpu}, separators=(',',':')) | |
# pwd incorrect | |
else: | |
now = datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S') | |
f = open("../raspiweb/access.log", "ab") | |
f.write(now + " : " + cgi.escape(os.environ["REMOTE_ADDR"]) + " : incorrect password\n") | |
f.close() | |
data = json.dumps({'pwdvalid': 0}, separators=(',',':')) | |
# http response with the json object | |
print("Content-type: text/html\n") | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment