def webserver_init(): printLog("simple web server") print(ver) printFree() try: import usocket as socket except: import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) printFree() return s
def webserver_run(s): printLog("> run:") while True: webconn(s)
def webconn(s): global trySetup printLog("> webconn ") led.blink(50) global wnum, web_info, ssidTemp, passTemp, web_wifi conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) request = str(request) try: rs = request.split(" ")[1] rs = (rs[2:]).split("&") ssidTemp = rs[0].split("=") passTemp = rs[1].split("=") if ssidTemp[0] == "ssid": ssidTemp = ssidTemp[1] print("ssid.ok") if len(ssidTemp) > 1: web_info = "<i>last ssid from form: " + ssidTemp + "</i><hr />" if passTemp[0] == "pass": passTemp = passTemp[1] print("pass.ok") except: rs = "err" print() print('Content = ' + str(rs)) # led_on = request.find('/?led=on') print() print("wifi_config: ") from utils.wifi_connect import WiFiConnect wc = WiFiConnect() webWc = "<hr /><b>Saved networks: </b><br />" for k, v in wc.config['networks'].items(): webWc += k + "<br />" try: print("try save new netw.") print("ssid: " + str(ssidTemp) + " | pass: "******"ok") led.blink(1000) led.blink(1000) trySetup = False except: print("err") wnum += 1 web_wifi = webnets + webWc + "<br /> refresh (" + str(wnum) + ")" response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close()
def webserver_run(s): printLog("> web server run:") while trySetup: webconn(s)