forked from Bingmang/gpu-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (36 loc) · 1.04 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
import json
import time
import yaml
import os
from flask import Flask, request, render_template, send_file
with open(os.path.join(os.path.split(__file__)[0], 'config.yaml')) as f:
config = yaml.safe_load(f)
app = Flask(__name__)
act_map = {}
@app.route('/')
def index():
return send_file('templates/index.html')
@app.route('/api/remove', methods=['post'])
def remove():
if request.method == 'POST':
host = request.form.get('host', "")
if host in act_map:
act_map.pop(host)
print(f'remove {host}')
return str(0)
@app.route('/api/gpu', methods=['GET'])
def gpu():
return json.dumps(act_map)
@app.route('/api/ping', methods=['GET', 'POST'])
def ping():
body = 0
if request.method == 'POST':
data = request.get_data()
data = json.loads(data.decode())
if not data.get('ip'):
body = 1
else:
act_map[data['host']] = data
return str(body)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(config['lab']['center']['port']))