-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapi_rec_request.py
More file actions
37 lines (32 loc) · 948 Bytes
/
api_rec_request.py
File metadata and controls
37 lines (32 loc) · 948 Bytes
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
# use web interface to display HTTP Request message
import utime
import network
import socket
import urequests
from NetworkCredentials import NetworkCredentials
from WiFiConnection import WiFiConnection
# connect to WiFi
if not WiFiConnection.start_station_mode(True):
raise RuntimeError('network connection failed')
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# main loop
while True:
client, client_addr = s.accept()
raw_request = client.recv(1024)
print("*** byte string ***")
print()
print(raw_request)
print()
# translate byte string to normal string variable
decoded_request = raw_request.decode("utf-8")
print("*** Decoded string ***")
print()
print(decoded_request)
print()
client.send("HTTP/1.1 200 OK\r\n\r\nRequest Received\r\n")
client.close()