-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysite.py
118 lines (93 loc) · 3.59 KB
/
mysite.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
import eventlet, os
from eventlet import wsgi, websocket, greenthread
execTimer = False
@websocket.WebSocketWSGI
def startTimer(ws):
n_cnt = 0
global execTimer
while execTimer:
print('Timer fired! {}'.format(n_cnt))
greenthread.sleep(1)
n_cnt+=1
try:
ws.send('Timer fired! {}'.format(n_cnt))
except Exception as e:
print('Client websocket not available')
ws.close()
return
@websocket.WebSocketWSGI
def processMessage(ws):
m = ws.wait()
print('Message received: {}'.format(m))
@websocket.WebSocketWSGI
def saveData(ws):
filename = ws.wait()
print('Filename: {}'.format(filename))
data = ws.wait()
data_size = float(len(data)) / 1000 #kb
print('Sizeof data: {:.1f} kb'.format(data_size))
new_file = os.path.join(os.path.expanduser('~'), filename)
print('Upload saved to: {}'.format(new_file))
with open(new_file, 'wb') as file:
file.write(data)
def dispatch(environ, start_response):
"""
WEBSOCKETS
"""
global execTimer
if environ['PATH_INFO'] == '/data':
print('PATH_INFO == \'/data\'')
return saveData(environ, start_response)
elif environ['PATH_INFO'] == '/message':
print('PATH_INFO == \'/message\'')
return processMessage(environ, start_response)
elif environ['PATH_INFO'] == '/timer':
print('PATH_INFO == \'/timer\'')
if execTimer:
execTimer = False
start_response('200 OK', [])
return []
else:
execTimer = True
return startTimer(environ, start_response)
"""
STANDARD HTML ENDPOINTS
"""
elif environ['PATH_INFO'] == '/':
print('PATH_INFO == \'/\'')
start_response('200 OK', [('content-type', 'text/html')])
return [open(os.path.join(os.path.dirname(__file__),
'mysite/templates/WASM_Client.html')).read()]
elif environ['PATH_INFO'] == '/qtloader.js':
print('PATH_INFO == \'/qtloader.js\'')
str_data = open(os.path.join(os.path.dirname(__file__),
'mysite/static/qtloader.js')).read()
start_response('200 OK', [('content-type', 'application/javascript') ])
return [str_data]
elif environ['PATH_INFO'] == '/qtlogo.svg':
print('PATH_INFO == \'/qtlogo.svg\'')
img_data = open(os.path.join(os.path.dirname(__file__),
'mysite/static/qtlogo.svg'), 'rb').read()
start_response('200 OK', [('content-type', 'image/svg+xml'),
('content-length', str(len(img_data)))])
return [img_data]
elif environ['PATH_INFO'] == '/WASM_Client.js':
print('PATH_INFO == \'/WASM_Client.js\'')
str_data = open(os.path.join(os.path.dirname(__file__),
'mysite/static/WASM_Client.js')).read()
start_response('200 OK', [('content-type', 'application/javascript')])
return [str_data]
elif environ['PATH_INFO'] == '/WASM_Client.wasm':
print('PATH_INFO == \'/WASM_Client.wasm\'')
bin_data = open(os.path.join(os.path.dirname(__file__),
'mysite/static/WASM_Client.wasm'), 'rb').read()
start_response('200 OK', [('content-type', 'application/wasm')])
return [bin_data]
else:
path_info = environ['PATH_INFO']
print('PATH_INFO = {}'.format(path_info))
return None
if __name__ == '__main__':
listener = eventlet.listen(('127.0.0.1', 7000))
print('\nVisit http://localhost:7000/ in your websocket-capable browser.\n')
wsgi.server(listener, dispatch)