#!/usr/bin/env python3 import cgi, os, json, sys LOG = "radioimplicancies.log" MAX_ROWS = 3 method = os.environ.get("REQUEST_METHOD", "GET").upper() if method == "POST": data = json.load(sys.stdin) if MAX_ROWS: rows = [] with open(LOG) as f: for line in f: row = json.loads(line) rows.append(row) rows = rows[-(MAX_ROWS-1):] with open(LOG, "w") as f: for row in rows: print(json.dumps(row), file=f) print (json.dumps(data), file=f) else: with open(LOG, "a") as f: print(json.dumps(data), file=f) print ("Content-type: text/html; charset=utf-8") print () print ("ok") sys.exit(0) # debugging # print ("Content-type: text/html; charset=utf-8") # print () out = [] with open(LOG) as f: for line in f: data = json.loads(line) out.append(data) print ("Content-type: application/json") print () print (json.dumps(out, indent=2))