-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
99 lines (76 loc) · 2.71 KB
/
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
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
from datetime import datetime
from flask import Flask, jsonify, render_template
from flask_caching import Cache
from flask import request
from http import HTTPStatus
config = {
"DEBUG": True, # some Flask specific configs
"CACHE_TYPE": "SimpleCache", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 300
}
app = Flask(__name__)
cache = Cache(app, config=config)
@app.route('/talk', methods=['GET'])
def client_entry_point():
clientTime= ''
cmd = None
clients = cache.get("clients")
if clients is None:
clients = {}
clientID = request.args.get('clientID')
status = request.args.get('status')
if clientID not in clients:
clients[clientID] = {'ID': clientID,
'status': status,
'cmd': None,
'timestamp': datetime.now(),
'last_request':0,}
else:
# Update status
clients[clientID]['status'] = status
# if shutdown remove client
if status == 'shutdown':
clients.pop(clientID)
else:
# Update timestamp
clients[clientID]['timestamp'] = datetime.now()
# Get command and reset it (so its only send once)
cmd = clients[clientID]['cmd']
clients[clientID]['cmd'] = None
cache.set("clients", clients)
d = {'code': 200, 'RequestTime': clientTime, 'cmd': cmd, }
return jsonify(d)
# Endpoint to set shutdown command
@app.route('/<clientID>/shutdown', methods=['GET'])
def shutdown_client(clientID=None):
clients = cache.get("clients")
# Check if clientID is valid
if clientID is None or clientID not in clients:
return '', HTTPStatus.BAD_REQUEST
# Set command to shutdown
clients[clientID]['cmd'] = 'shutdown'
cache.set("clients", clients)
return '', HTTPStatus.NO_CONTENT
# Show dashboard clients
@app.route('/clients')
def clients():
clients = cache.get("clients")
clients_to_remove = []
if clients:
# calc last request time
for id, client in clients.items():
client['last_request'] = (datetime.now() - client['timestamp']).total_seconds()
# remove client if last request was more than 10 seconds ago
if client['last_request'] > 10:
clients_to_remove.append(id)
# remove clients
for id in clients_to_remove:
clients.pop(id)
return render_template('clients.html',clients=clients)
# Show dashboard
@app.route('/')
def dashboard():
clients = cache.get("clients")
return render_template('dashboard.html',clients=clients)
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)