Skip to content

Instantly share code, notes, and snippets.

@h3xagn
Last active March 21, 2022 09:22
Show Gist options
  • Save h3xagn/a8ed4779c2b67c731d1dbc1e1f32344b to your computer and use it in GitHub Desktop.
Save h3xagn/a8ed4779c2b67c731d1dbc1e1f32344b to your computer and use it in GitHub Desktop.
Build ETL from device to cloud: https://h3xagn.com
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from datetime import datetime
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
print(f"\nNew POST received at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}. Processing...")
content_length = int(self.headers["Content-Length"])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
print(f"- Sent response 200 OK.")
json_string = body.decode("utf8").replace("'", '"')
data = json.loads(json_string)
filename = f"./data/data_{data['sn']}_{datetime.now().timestamp()}.json"
with open(filename, "w") as jsonfile:
json.dump(data, jsonfile)
print(f"- JSON data saved: {filename}.")
print(f"Done.")
print("Server started. Ready.")
httpd = HTTPServer(("127.0.0.1", 8443), SimpleHTTPRequestHandler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment