forked from MTN-RowinAndruscavage/STEM4T-Display-ESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_web_server.py
68 lines (54 loc) · 1.34 KB
/
_web_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
## Creates a Wifi access point
## Serves a web page with the device's name and a random fortune cookie
ap_ssid="TDweb-%s" % username
ap_password="5GforAll"
fortunes = [
"This is the first",
"Have fortune #2",
"Third time is a charm"
]
import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ap_ssid, password=ap_password)
try:
import usocket as socket
except:
import socket
import esp
esp.osdebug(None)
import random
import gc
gc.collect()
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
TD.clear()
TD.typeset("SSID: %s\nPW: %s\nhttp://%s/" % (ap_ssid, ap_password, ap.ifconfig()[0]))
def web_page():
html = """<html><head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Welcome to %s's server</h1>
%s
</body>
</html>""" % (username, random.choice(fortunes))
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
# Display web hits
webcounter = 0
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
response = web_page()
conn.send(response)
conn.close()
webcounter += 1
TD.typeset("Page views: %d" % webcounter, 0, 3)
TD.typeset("Last from: %s" % addr[0], 0, 4)