From 8db1c371d1fa13f5488443c00b320b95857259ab Mon Sep 17 00:00:00 2001 From: Chris Miller Date: Mon, 9 Apr 2018 16:39:43 -0600 Subject: [PATCH] fix for out of memory on esp8266 --- wifimgr.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/wifimgr.py b/wifimgr.py index 93740c7..59205f2 100644 --- a/wifimgr.py +++ b/wifimgr.py @@ -3,6 +3,7 @@ import socket import ure import time + ap_ssid = "WifiManager" ap_password = "tayfunulu" ap_authmode = 3 # WPA2 @@ -99,22 +100,31 @@ def do_connect(ssid, password): return connected -def send_response(client, payload, status_code=200): +def send_header(client, status_code=200, content_length=None ): client.sendall("HTTP/1.0 {} OK\r\n".format(status_code)) client.sendall("Content-Type: text/html\r\n") - client.sendall("Content-Length: {}\r\n".format(len(payload))) + if content_length is not None: + client.sendall("Content-Length: {}\r\n".format(content_length)) + client.sendall("\r\n") - if len(payload) > 0: +def send_response(client, payload, status_code=200): + content_length = len(payload) + + send_header(client, status_code, content_length) + + if content_length > 0: client.sendall(payload) + client.close() def handle_root(client): wlan_sta.active(True) ssids = sorted(ssid.decode('utf-8') for ssid, *_ in wlan_sta.scan()) - response = [] - response.append("""\ + send_header(client) + + client.sendall("""\

@@ -126,8 +136,10 @@ def handle_root(client): """) - for ssid in ssids: - response.append("""\ + while len(ssids): + ssid = ssids.pop(0) + + client.sendall("""\ {0} @@ -135,7 +147,7 @@ def handle_root(client): """.format(ssid)) - response.append("""\ + client.sendall("""\ Password: @@ -171,8 +183,8 @@ def handle_root(client): """ % dict(filename=NETWORK_PROFILES)) - send_response(client, "\n".join(response)) + client.close() def handle_configure(client, request): match = ure.search("ssid=([^&]*)&password=(.*)", request)