Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ubinascii
- import utime
- import socket
- import select
- html = """<!DOCTYPE html>
- <html>
- <head> <title>Ouroboros IoT Login</title> </head>
- <body>
- <form action="configure.html" method="POST">
- Username : <input type="text" name="username"></br>
- Password: <input type="password" name="password" ></br>
- <input type="submit" value="submit" name="submit">
- </form>
- </body>
- </html>
- """
- # Find out the post parameters in a dictionary
- def get_post_params(req):
- print("Inside POST PARAMS : req = " + req)
- post_params = req.split('rn')[-1:][0]
- # Check if the post body contains the necessary fields
- # Split the post_params by &
- # params : ['username=', 'password=', 'method=POST', 'url=http%3A%2F%2Ftwig-me.com%2Fv1%2Fusergroups%2FWKMUYXELA9LCC', 'jsondata=', 'submit=submit']
- print("post_params : " + post_params)
- params = post_params.split('&')
- print("Params")
- print(params)
- # Initialize the key value pair dict
- post_dict = {}
- # Iterate on each param
- for param in params:
- # Each param would be like 'method=POST', etc
- key_val = param.split('=')
- print("Key Val :")
- print(key_val)
- key = key_val[0]
- val = key_val[1]
- # Update post_dict
- post_dict[key] = val
- return post_dict
- # This web server takes care of the WiFi configuration
- # max_run_sec
- def web_server(max_run_sec = None):
- # Create server socket
- addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
- s = socket.socket()
- # TODO : If both the wifi and sta are operating simultaneously, then bind only to WiFi
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.bind(addr)
- s.listen(1)
- poller = select.poll()
- poller.register(s, select.POLLIN)
- startTimeEpoch = utime.time()
- while True:
- all_events = poller.poll(200) # time in milliseconds
- if len(all_events) > 0:
- try:
- print("Just after GC Collect!")
- gc.collect()
- #print("Just before accepting")
- res = s.accept()
- client_s = res[0]
- client_addr = res[1]
- req = ''
- req = client_s.recv(4096)
- req = req.decode()
- req = str(req)
- # Came here means that there has been some connection!
- # Reset the start time epoch in such a case:
- startTimeEpoch = utime.time()
- # Check route now
- if req.find('configure.html') != -1:
- print("Got configure request!rn")
- # Check if the username and password are correct, if not, configure:
- login_config = get_login_config()
- username = login_config['user']
- pwd = login_config['password']
- print("Username : " + username + ", pwd : " + pwd)
- # Find the POST PARAMETERS sent
- # There would be just one entry in the array, so get the 0th index directly
- # post_params : 'username=&password=&method=POST&url=http%3A%2F%2Fjam-me.com%2Fv1%2Fusergroups%2FWKMUYXELA9LCC&jsondata=&submit=submit'
- post_dict = get_post_params(req)
- # Now check if the post_dict has the key and value for username and password as needed?
- username_post = post_dict['username']
- password_post = post_dict['password']
- # Check if the password is same as expected
- if (username_post == username) and (password_post == pwd):
- hidden_input = '<input type="hidden" name="username" value="' + username + '"><input type="hidden" name="passphrase" value="' + pwd + '">'
- # Send the login username and password inside the hidden input field
- configure_html = "<!DOCTYPE html><html><head> <title>Ouroboros IoT WiFi Configuration Page</title> </head><body><form action="configure_wifi.html" method="POST">WiFi SSID : <input type="text" name="essid"></br>WiFi Password: <input type="password" name="passphrase" ></br>" + hidden_input + "<input type="submit" value="submit" name="submit"></form></body></html>"
- # TODO : Also show link to webpage, where from we can change the login credentials
- client_s.send(configure_html)
- else:
- client_s.send(login_fail_html)
- elif req.find('configure_wifi.html') != -1:
- # Check if the username and password are correct, if not, configure:
- login_config = get_login_config()
- username = login_config['user']
- pwd = login_config['password']
- # Get post parameters
- post_dict = get_post_params(req)
- # Now check if the post_dict has the key and value for username and password as needed?
- username_post = post_dict['username']
- password_post = post_dict['password']
- # Check if the password is same as expected
- if (username_post == username) and (password_post == pwd):
- # Do some sanity check for handling the new wifi ssid and password
- new_wifi_ssid = post_dict['essid']
- new_wifi_passphrase = post_dict['passphrase']
- # Set the wifi credentials
- save_wifi_config(new_wifi_ssid, new_wifi_passphrase)
- client_s.send('<!DOCTYPE html><html><head> <title>Ouroboros IoT WiFi Configuration Success</title> </head><body>Configuration successful!<br>Device would go into reboot now!</body></html>')
- # Reboot device now
- machine.reset()
- else:
- client_s.send(login_fail_html)
- elif req.find('index.html') != -1:
- print("Got index.html request!rn")
- client_s.send(html)
- else :
- # Do nothing
- print("Invalid request received! Show the login page again!rn")
- client_s.send(html)
- except OSError:
- # Got no request and it timedout!
- print("Timed-out, no request received!rn")
- except Exception as e:
- print("Got some exceptionrn")
- print(str(e))
- finally:
- client_s.close()
- if max_run_sec is not None:
- elapsedTime = utime.time() - startTimeEpoch
- if elapsedTime > max_run_sec:
- # Max run time of web server has elapsed, time to exit this mode!
- break
- # Unregister poller
- poller.unregister(s)
- # When while loop ends!
- Inside POST PARAMS : req = GET /configure.html HTTP/1.1
- Host: 192.168.0.1
- Connection: keep-alive
- Cache-Control: max-age=0
- Upgrade-Insecure-Requests: 1
- User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
- Accept-Encoding: gzip, deflate
- Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Add Comment
Please, Sign In to add comment