-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathALTweb_server.py
76 lines (67 loc) · 2.43 KB
/
ALTweb_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from urllib.parse import urlparse, parse_qs
import socket
import wifiCfg
import network
from ALTconnection import *
from ALTutils import urlDecode, extract_string
def get_user_input(port=80, alert_msg='Navigate to:', AP=False):
"""
Hosts a webpage on the specified port to gather user input / Enables AP mode.
Parameters
----------
port : int
Number of port to be used for connection (default : 80)
alert_msg : msg
Alert message to be displayed to the user (default : 'Navigate to:')
AP : bool
Access Point mode (default : False)
Returns
-------
The string submitted by the user
"""
ip = ['192.168.4.1']
if AP:
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='ESP32')
ap.config(authmode=3, password='123456789')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip[0], port))
else:
check_connection()
ip = wifiCfg.wlan_sta.ifconfig()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip[0], port))
s.listen(5)
print(alert_msg + ' http://' + str(ip[0]) + ':' + str(port) + '/submit')
while 1:
status_code = '200 OK'
input_var = None
conn, addr = s.accept()
request = conn.recv(1024)
request = urlDecode(str(request))
parsed_url = urlparse(extract_string(request, 'GET '))
requested_path = parsed_url.path
if requested_path == '/submit':
input_var = parse_qs(parsed_url.query).get('input', [None])[0]
if input_var:
with open("./Assets/HTML/submitted.html", "r") as html_file:
response = html_file.read()
else:
with open("./Assets/HTML/submit.html", "r") as html_file:
response = html_file.read()
elif requested_path == '/favicon.ico':
with open("./Assets/HTML/favicon.ico", "rb") as html_file:
response = html_file.read()
else:
with open("./Assets/HTML/not_found.html", "r") as html_file:
response = html_file.read()
status_code = '404 Not Found'
conn.send('HTTP/1.1 {}\n'.format(status_code))
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
if input_var:
s.close()
return input_var