-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.py
87 lines (64 loc) · 1.82 KB
/
app.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
import io
from flask import Flask, Response, request, jsonify, render_template
try:
from werkzeug.wsgi import FileWrapper
except Exception as e:
from werkzeug import FileWrapper
global STATE
STATE = {}
app = Flask(__name__)
''' Client '''
@app.route('/')
def root():
return render_template('/index.html')
@app.route('/rd', methods=['POST'])
def rd():
req = request.get_json()
key = req['_key']
if req['filename'] == STATE[key]['filename']:
attachment = io.BytesIO(b'')
else:
attachment = io.BytesIO(STATE[key]['im'])
w = FileWrapper(attachment)
resp = Response(w, mimetype='text/plain', direct_passthrough=True)
resp.headers['filename'] = STATE[key]['filename']
return resp
@app.route('/event_post', methods=['POST'])
def event_post():
global STATE
req = request.get_json()
key = req['_key']
STATE[key]['events'].append(request.get_json())
return jsonify({'ok': True})
''' Remote '''
@app.route('/new_session', methods=['POST'])
def new_session():
global STATE
req = request.get_json()
key = req['_key']
STATE[key] = {
'im': b'',
'filename': 'none.png',
'events': []
}
return jsonify({'ok': True})
@app.route('/capture_post', methods=['POST'])
def capture_post():
global STATE
with io.BytesIO() as image_data:
filename = list(request.files.keys())[0]
key = filename.split('_')[1]
request.files[filename].save(image_data)
STATE[key]['im'] = image_data.getvalue()
STATE[key]['filename'] = filename
return jsonify({'ok': True})
@app.route('/events_get', methods=['POST'])
def events_get():
req = request.get_json()
key = req['_key']
events_to_execute = STATE[key]['events'].copy()
STATE[key]['events'] = []
return jsonify({'events': events_to_execute})
if __name__ == '__main__':
#app.run('0.0.0.0', debug=True)
app.run('0.0.0.0')