-
Notifications
You must be signed in to change notification settings - Fork 1
/
ap.py
113 lines (82 loc) · 2.52 KB
/
ap.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import wifi
import socketpool
import led
import secrets
import hid
MAXBUF = 8192
def parse(url):
encoding_map = {
'%20': ' ',
'%21': '!',
'%22': '"',
'%23': '#',
'%24': '$',
'%25': '%',
'%26': '&',
'%27': "'",
'%28': '(',
'%29': ')',
'%2A': '*',
'%2B': '+',
'%2C': ',',
'%2D': '-',
'%2E': '.',
'%2F': '/',
'%3A': ':',
'%3B': ';',
'%3C': '<',
'%3D': '=',
'%3E': '>',
'%3F': '?',
'%40': '@',
'%5B': '[',
'%5C': '\\',
'%5D': ']',
'%5E': '^',
'%5F': '_',
'%60': '`',
'%7B': '{',
'%7C': '|',
'%7D': '}',
'%7E': '~',
'%C3%A4': 'ä',
'%C3%84': 'Ä',
'%C3%B6': 'ö',
'%C3%96': 'Ö',
'%C3%BC': 'ü',
'%C3%9C': 'Ü'
}
for encoded_char, decoded_char in encoding_map.items():
url = url.replace(encoded_char, decoded_char)
return url
def start_ap():
global pool
wifi.radio.start_ap(ssid=secrets.ap_ssid, password=secrets.ap_password)
# print access point settings
print("SSID: {}, password: {}".format(secrets.ap_ssid, secrets.ap_password))
print("IP: ", wifi.radio.ipv4_address_ap)
pool = socketpool.SocketPool(wifi.radio)
led.blink(2, 0.1)
def http_server():
server_socket = pool.socket()
server_socket.bind((str(wifi.radio.ipv4_address_ap), 80))
server_socket.listen(1)
print("Listening on {}:80".format(wifi.radio.ipv4_address_ap))
buf = bytearray(MAXBUF)
while True:
#print("Waiting for a connection...")
client_socket, client_address = server_socket.accept()
#print("Accepted connection from:", client_address)
size = client_socket.recv_into(buf, MAXBUF)
if b"GET /submit?data=" in buf:
resp = buf.decode().replace("GET /submit?data=", "")
buf = bytearray(MAXBUF)
payload = parse(resp[:resp.find("HTTP/1")]).split("%0A")
for line in payload:
hid.parseLine(line)
with open("index.html", "r") as html:
response = html.read()
content_length = len(response)
client_socket.sendall(f"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: {content_length}\r\n\r\n")
client_socket.sendall(response.encode())
client_socket.close()