def main(): Status.log('Runnning HTTP server') s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.settimeout(1) s.listen(5) while True: Status.log(inHttpRequest=" ") try: conn, addr = s.accept() HTTP.init(conn, addr) except: mainloop() continue Status.log('Got a connection from %s' % (str(addr)), inHttpRequest="o") if not HTTP.Valid: HTTP.error(400, "Bad request") continue if Status.flags["inFactoryreset"] == "!" and HTTP.checkRequest('/'): if HTTP.checkRequest('/setup'): HTTP.redirect("/setup") if HTTP.checkRequest('/'): HTTP.home() if HTTP.checkRequest('/setup'): HTTP.setup() elif HTTP.checkRequest('/setup', method='POST'): Status.log("Setup POST page") try: cfg = HTTP.getRequestVar("cfg") json = HTTP.urlDecode(cfg) json = ujson.loads(json) Config.load(json) Config.write() HTTP.OK("Setup OK") machine.reset() except ValueError: HTTP.error(400, "Bad JSON") elif HTTP.checkRequest('/flowui', method='GET'): Status.log("Reverting to flowUI") try: s = os.remove("boot.py") HTTP.OK("Reverted to FlowUI.") machine.reset() except ValueError: HTTP.error(400, "Cannot remove boot.py") elif HTTP.checkRequest('/factory-defaults', method='GET'): Status.log("Reverting to factory Defaults") try: s = os.remove("_settings.py") HTTP.OK("Factory defaults done.") machine.reset() except ValueError: HTTP.error(400, "Cannot remove _settings.py") elif HTTP.checkRequest('/push-file', method='POST'): file = HTTP.getRequestVar("file") if file: HTTP.begin(200, ctype="text/plain", html=False) HTTP.send(file) HTTP.end(html=False) continue if data: Status.log("Writing file %s" % file) s = open(file, "w") s.write(data) HTTP.OK("File written OK", msg=data) continue HTTP.error(400, "Bad request") # /led/set/<stripe>/<led>/<color> # /led/set/0/1/ffffff elif HTTP.checkRequest('/led/set'): stripe = HTTP.getRequestVar("stripe") led = HTTP.getRequestVar("led") color = HTTP.getRequestVar("color") if color and Leds.setColor(stripe, led, Leds.fromHtml(color)): HTTP.OK() else: HTTP.error(400, "Error in request") else: HTTP.error(404, "Not found")