-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
187 lines (150 loc) · 6.45 KB
/
web.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from MicroWebSrv2 import *
from time import sleep
from _thread import allocate_lock
# ============================================================================
# ============================================================================
# ============================================================================
@WebRoute(GET, '/test-redir')
def RequestTestRedirect(microWebSrv2, request) :
request.Response.ReturnRedirect('/test.pdf')
# ============================================================================
# ============================================================================
# ============================================================================
@WebRoute(GET, '/test-post', name='TestPost1/2')
def RequestTestPost(microWebSrv2, request) :
content = """\
<!DOCTYPE html>
<html>
<head>
<title>POST 1/2</title>
</head>
<body>
<h2>MicroWebSrv2 - POST 1/2</h2>
User address: %s<br />
<form action="/test-post" method="post">
First name: <input type="text" name="firstname"><br />
Last name: <input type="text" name="lastname"><br />
<input type="submit" value="OK">
</form>
</body>
</html>
""" % request.UserAddress[0]
request.Response.ReturnOk(content)
# ------------------------------------------------------------------------
@WebRoute(POST, '/test-post', name='TestPost2/2')
def RequestTestPost(microWebSrv2, request) :
data = request.GetPostedURLEncodedForm()
try :
firstname = data['firstname']
lastname = data['lastname']
except :
request.Response.ReturnBadRequest()
return
content = """\
<!DOCTYPE html>
<html>
<head>
<title>POST 2/2</title>
</head>
<body>
<h2>MicroWebSrv2 - POST 2/2</h2>
Hello %s %s :)<br />
</body>
</html>
""" % ( MicroWebSrv2.HTMLEscape(firstname),
MicroWebSrv2.HTMLEscape(lastname) )
request.Response.ReturnOk(content)
# ============================================================================
# ============================================================================
# ============================================================================
def OnWebSocketAccepted(microWebSrv2, webSocket) :
print('Example WebSocket accepted:')
print(' - User : %s:%s' % webSocket.Request.UserAddress)
print(' - Path : %s' % webSocket.Request.Path)
print(' - Origin : %s' % webSocket.Request.Origin)
if webSocket.Request.Path.lower() == '/wschat' :
WSJoinChat(webSocket)
else :
webSocket.OnTextMessage = OnWebSocketTextMsg
webSocket.OnBinaryMessage = OnWebSocketBinaryMsg
webSocket.OnClosed = OnWebSocketClosed
# ============================================================================
# ============================================================================
# ============================================================================
def OnWebSocketTextMsg(webSocket, msg) :
print('WebSocket text message: %s' % msg)
webSocket.SendTextMessage('Received "%s"' % msg)
# ------------------------------------------------------------------------
def OnWebSocketBinaryMsg(webSocket, msg) :
print('WebSocket binary message: %s' % msg)
# ------------------------------------------------------------------------
def OnWebSocketClosed(webSocket) :
print('WebSocket %s:%s closed' % webSocket.Request.UserAddress)
# ============================================================================
# ============================================================================
# ============================================================================
global _chatWebSockets
_chatWebSockets = [ ]
global _chatLock
_chatLock = allocate_lock()
# ------------------------------------------------------------------------
def WSJoinChat(webSocket) :
webSocket.OnTextMessage = OnWSChatTextMsg
webSocket.OnClosed = OnWSChatClosed
addr = webSocket.Request.UserAddress
with _chatLock :
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s HAS JOINED THE CHAT>' % addr)
_chatWebSockets.append(webSocket)
webSocket.SendTextMessage('<WELCOME %s:%s>' % addr)
# ------------------------------------------------------------------------
def OnWSChatTextMsg(webSocket, msg) :
addr = webSocket.Request.UserAddress
with _chatLock :
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s> %s' % (addr[0], addr[1], msg))
# ------------------------------------------------------------------------
def OnWSChatClosed(webSocket) :
addr = webSocket.Request.UserAddress
with _chatLock :
if webSocket in _chatWebSockets :
_chatWebSockets.remove(webSocket)
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s HAS LEFT THE CHAT>' % addr)
# ============================================================================
# ============================================================================
# ============================================================================
print()
# Loads the PyhtmlTemplate module globally and configure it,
pyhtmlMod = MicroWebSrv2.LoadModule('PyhtmlTemplate')
pyhtmlMod.ShowDebug = True
pyhtmlMod.SetGlobalVar('TestVar', 12345)
# Loads the WebSockets module globally and configure it,
wsMod = MicroWebSrv2.LoadModule('WebSockets')
wsMod.OnWebSocketAccepted = OnWebSocketAccepted
# Instanciates the MicroWebSrv2 class,
mws2 = MicroWebSrv2()
# SSL is not correctly supported on MicroPython.
# But you can uncomment the following for standard Python.
# mws2.EnableSSL( certFile = 'SSL-Cert/openhc2.crt',
# keyFile = 'SSL-Cert/openhc2.key' )
# For embedded MicroPython, use a very light configuration,
mws2.SetEmbeddedConfig()
# All pages not found will be redirected to the home '/',
mws2.NotFoundURL = '/'
# Starts the server as easily as possible in managed mode,
mws2.StartManaged()
# Main program loop until keyboard interrupt,
try :
while mws2.IsRunning :
sleep(1)
except KeyboardInterrupt :
pass
# End,
print()
mws2.Stop()
print('Bye')
print()
# ============================================================================
# ============================================================================
# ============================================================================