-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_server.py
63 lines (51 loc) · 1.37 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
import network
import socket
ssid = "Smart Chess"
password = "123456789"
def connect():
# Set up Access Point
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password) # Set your desired SSID here
ap.active(True)
while not ap.active():
pass
print('Access Point active with IP:', ap.ifconfig())
def open_socket():
# Open a socket
address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
connection = socket.socket()
connection.bind(address)
connection.listen(5)
print('Listening on', address)
return connection
def webpage():
#Template HTML
html = """<!DOCTYPE html>
<html>
<head>
<title>Pico W Webpage</title>
</head>
<body>
<h1>Hello from Pico W!</h1>
</body>
</html>
"""
return str(html)
def serve(connection, html):
while True:
cl, addr = connection.accept()
print('Client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
cl.send(html)
cl.close()
try:
connect()
connection = open_socket()
html = webpage()
serve(connection, html)
except KeyboardInterrupt:
machine.reset()