-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
91 lines (76 loc) · 3.27 KB
/
start.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
from microWebSrv import MicroWebSrv
# ----------------------------------------------------------------------------
@MicroWebSrv.route('/test')
def _httpHandlerTestGet(httpClient, httpResponse) :
content = """\
<!DOCTYPE html>
<html lang=en>
<head><meta charset="UTF-8" /><title>TEST GET</title></head>
<body>
<h1>TEST GET</h1>
Client IP address = %s<br /> # httpClient.GetIPAddr()
<form action="/test" method="post" accept-charset="ISO-8859-1">
First name: <input type="text" name="firstname"><br />
Last name: <input type="text" name="lastname"><br />
<input type="submit" value="Submit"></form>
</body>
</html>
""" % httpClient.GetIPAddr()
# publie la page GET
httpResponse.WriteResponseOk(headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content)
@MicroWebSrv.route('/test', 'POST')
def _httpHandlerTestPost(httpClient, httpResponse):
formData = httpClient.ReadRequestPostedFormData()
firstname = formData["firstname"]
lastname = formData["lastname"]
content = """\
<!DOCTYPE html>
<html lang=en><head><meta charset="UTF-8" /><title>TEST POST</title></head>
<body><h1>TEST POST</h1>
Firstname = %s<br /> # MicroWebSrv.HTMLEscape(firstname)
Lastname = %s<br /> # MicroWebSrv.HTMLEscape(lastname)
</body></html>
""" % (MicroWebSrv.HTMLEscape(firstname), MicroWebSrv.HTMLEscape(lastname))
# publie la page POST
httpResponse.WriteResponseOk(headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content)
@MicroWebSrv.route('/edit/<index>') # <IP>/edit/123 -> args['index']=123
@MicroWebSrv.route('/edit/<index>/abc/<foo>') # <IP>/edit/123/abc/bar -> args['index']=123 args['foo']='bar'
@MicroWebSrv.route('/edit') # <IP>/edit -> args={}
def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :
content = """\
<!DOCTYPE html>
<html lang=en>
<head><meta charset="UTF-8" /><title>TEST EDIT</title></head>
<body>
"""
content += "<h1>EDIT item with {} variable arguments</h1>".format(len(args))
if 'index' in args :
content += "<p>index = {}</p>".format(args['index'])
if 'foo' in args :
content += "<p>foo = {}</p>".format(args['foo'])
content += """
</body>
</html>
"""
httpResponse.WriteResponseOk(headers = None, contentType = "text/html", contentCharset = "UTF-8", content = content)
# ----------------------------------------------------------------------------
def _acceptWebSocketCallback(webSocket, httpClient) :
print("WS ACCEPT")
webSocket.RecvTextCallback = _recvTextCallback
webSocket.RecvBinaryCallback = _recvBinaryCallback
webSocket.ClosedCallback = _closedCallback
def _recvTextCallback(webSocket, msg) :
print("WS RECV TEXT : %s" % msg)
webSocket.SendText("Reply for %s" % msg)
def _recvBinaryCallback(webSocket, data) :
print("WS RECV DATA : %s" % data)
def _closedCallback(webSocket) :
print("WS CLOSED")
# ----------------------------------------------------------------------------
#routeHandlers = [("/test", "GET", httpHandlerTestGet),("/test", "POST", httpHandlerTestPost)]
srv = MicroWebSrv(webPath='www/')
srv.MaxWebSocketRecvLen = 256
srv.WebSocketThreaded = False
srv.AcceptWebSocketCallback = acceptWebSocketCallback
srv.Start()
# ----------------------------------------------------------------------------