#!/usr/bin/env python3 """Upload a firmware image to the controller over Ethernet. ota.py [build/led_controller_v2.bin] """ import http.client import json import os import sys host = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__) path = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(__file__), "..", "build", "led_controller_v2.bin") size = os.path.getsize(path) conn = http.client.HTTPConnection(host, 80, timeout=60) conn.request("GET", "/ota") print("running:", conn.getresponse().read().decode()) print(f"uploading {path} ({size} bytes)") with open(path, "rb") as f: conn.request("POST", "/ota", body=f, headers={"Content-Length": str(size), "Content-Type": "application/octet-stream"}) r = conn.getresponse() body = r.read().decode() print(r.status, body) sys.exit(0 if r.status == 200 else 1)