-
Notifications
You must be signed in to change notification settings - Fork 10
/
Simple_WebServer.py
120 lines (99 loc) · 2.99 KB
/
Simple_WebServer.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
114
115
116
117
118
119
120
ssid = '' # WiFi name
pw = '' # WiFi password
port = 80 # server port
conns = 1 # number of channels
from machine import Pin
import network, socket, sys
led = Pin(2, Pin.OUT, value=1)
# webpage template
html = """
<!DOCTYPE html>
<html>
<head>
<title>ESP8266 Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="data:," />
<style>
body {
background-color: Moccasin;
}
h1 {
color: SaddleBrown;
}
h2 {
color: Olive;
}
</style>
</head>
<body>
<center>
<h1>ESP8266 Web Server</h1>
<h2>LED status: <!--led_status--></h2>
<form methon="GET" action="">
<p><input id="led_on" type="submit" name="led" value="On" /></p>
<p><input id="led_off" type="submit" name="led" value="Off" /></p>
</form>
</center>
</body>
</html>
"""
# add HTTP response headers
http_resp = 'HTTP/1.1 200 OK\r\n' + \
'Content-Type: text/html\r\n' + \
'Connection: close\r\n\r\n'
# parse html to a single string
html = http_resp + ''.join([line.strip() for line in html.split('\n')])
# generated webpage to be sent to user
def web_page():
led_status = 'ON' if led.value() == 0 else 'OFF'
return html.replace('<!--led_status-->', led_status)
# extract url parameters from HTTP request
def get_paras(get_str):
para_dict = {}
q_pos = get_str.find('/?')
if q_pos > 0:
http_pos = get_str.find('HTTP/')
para_list = get_str[q_pos + 2 : http_pos - 1].split('&')
for para in para_list:
para_tmp = para.split('=')
para_dict.update({para_tmp[0] : para_tmp[1]})
return para_dict
# wifi error descriptions
wifi_error = {
network.STAT_WRONG_PASSWORD: 'wrong password',
network.STAT_NO_AP_FOUND: 'wifi AP not found',
-1: 'due to other problems',
}
# connect to wifi
print('Connecting to WiFi...')
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)
while wifi.status() == network.STAT_CONNECTING:
pass
if wifi.status() != network.STAT_GOT_IP:
print('Failed to connect:', wifi_error.get(wifi.status(), wifi_error[-1]))
sys.exit()
print('Connected.')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', port))
s.listen(conns)
print('Web server started on', 'http://{}:{}'.format(wifi.ifconfig()[0], port))
while True:
# wait for client
client, addr = s.accept()
print('Client connected from', addr[0])
request = client.recv(1024)
# extract url parameters
request_text = request.decode('utf-8')
paras = get_paras(request_text)
# control the led
led_status = paras.get('led', None)
if led_status == 'On':
led.value(0)
else:
led.value(1)
# send response (web page) to user
response = web_page()
client.send(response)
client.close()