-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
78 lines (63 loc) · 2.48 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
import json
import os
import random
import string
import requests
from Modules.model_access import tester
from Modules.utils import session_number, add_to_csv
from flask import Flask, render_template, request, send_from_directory, abort, jsonify
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "Modules/data"
# TODO: add seesion handling
@app.route("/post_data", methods=['POST'])
def get_post_data():
# Add the key-stroke timings to database
js_d_data = request.form['down_time_data']
js_u_data = request.form['up_time_data']
add_to_csv(json.loads(js_d_data), json.loads(js_u_data))
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
@app.route("/post_test", methods=['POST'])
def get_post_test():
# Authenticate the test
js_d_data = request.form['down_time_data']
js_u_data = request.form['up_time_data']
result = tester(json.loads(js_d_data), json.loads(js_u_data))
return jsonify(result=result)
@app.route('/upload/<path:filename>', methods=['POST'])
def upload(filename):
# Upload the time.csv
if os.path.exists("Modules/data/time.csv"):
hash_name = "".join( [random.choice(string.ascii_letters) for i in range(6)] )
data=open("Modules/data/time.csv", 'rb')
requests.put("http://s3-eu-central-1.amazonaws.com/ucontacti/keystroke/" + "time_" + hash_name + ".csv",
data=data)
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
else:
return jsonify(status="file not exist")
@app.route("/del_data", methods=['POST'])
def del_post():
# Delete current data
if os.path.exists("Modules/data/time.csv"):
os.remove("Modules/data/time.csv")
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
@app.route("/init", methods=['POST'])
def init_client():
# Init client status
if os.path.exists("Modules/data/time.csv"):
result = session_number()
else:
result = 0
return jsonify(session=result)
@app.route('/download/<path:filename>', methods=['POST'])
def download(filename):
# Download the time.csv
uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
if os.path.exists("Modules/data/time.csv"):
return send_from_directory(directory=uploads, filename=filename), 200
else:
return jsonify(status="file not exist")
@app.route("/")
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', debug='on')