-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_logger_server.py
executable file
·68 lines (48 loc) · 1.63 KB
/
sensor_logger_server.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
#!/usr/bin/env python3
import json
import time
import gzip
from pathlib import Path, PosixPath
from flask import Flask, request, Response
app = Flask(__name__)
logs = []
@app.route('/sensor_logger', methods=['POST'])
def sensor_logger():
request.json["timestamp"] = time.time()
request.json["ip_address"] = request.remote_addr
logs.append(request.json)
response_dict = {
"status": "success"
}
return Response(json.dumps(response_dict), status=201, mimetype='application/json')
@app.route('/export_to_internal_storage')
def export_to_internal_storage():
save_path = PosixPath("~/.local/share/sensor_logger/").expanduser()
Path(save_path).mkdir(parents=True, exist_ok=True)
file_location = f"{save_path}/{time.time()}.json.gz"
generated_json = json.dumps(logs)
with gzip.open(file_location, "w") as outfile:
outfile.write(bytes(generated_json, 'ascii'))
del logs[:]
response_dict = {
"status": "success",
"file_location": file_location
}
return Response(json.dumps(response_dict), status=201, mimetype='application/json')
@app.route('/export.json')
def export_json():
generated_json = json.dumps(logs)
del logs[:]
return Response(generated_json, status=200, mimetype='application/json')
@app.route('/export.json.gz')
def export_json_gzip():
generated_json = json.dumps(logs)
del logs[:]
gzip_bytes = gzip.compress(bytes(generated_json, 'ascii'))
return Response(gzip_bytes, status=200, mimetype='application/x-gzip')
if __name__ == '__main__':
app.run(
host='127.0.0.1',
port=3000,
debug=True
)