Browse Source

update 更新可以通过配置文件修改api的名称

v2
lxb 7 months ago
parent
commit
6fe811458e
  1. 2
      client.py
  2. 3
      client_config.json
  3. 23
      server.py
  4. 3
      server_config.json
  5. 4
      web/js/script.js

2
client.py

@ -205,7 +205,7 @@ def main():
# 持续发送 # 持续发送
send_interval = client_cfg['interval'] send_interval = client_cfg['interval']
api_url = client_cfg['server_url'] + '/api/update_data' api_url = client_cfg['server_url'] + f'/{client_cfg['api_name']}/update_data'
while True: while True:
data = collect_data() data = collect_data()
try: try:

3
client_config.json

@ -9,5 +9,6 @@
"/media/D", "/media/D",
"/media/E", "/media/E",
"/media/F" "/media/F"
] ],
"api_name": "api"
} }

23
server.py

@ -10,20 +10,28 @@ CORS(app)
server_cfg = None server_cfg = None
data_dict = dict() data_dict = dict()
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', default='server_config.json', type=str, help='the path of config json.')
args = parser.parse_args()
# 加载配置文件
cfg_path = args.cfg
with open(cfg_path, 'r') as f:
server_cfg = json.load(f)
#endregion #endregion
#region 接口 #region 接口
# 测试用 # 测试用
@app.route('/api') @app.route(f'/{server_cfg['api_name']}')
def hello(): def hello():
return 'hi. —— CheckGPUsWeb' return 'hi. —— CheckGPUsWeb'
@app.route('/api/get_data', methods=['GET']) @app.route(f'/{server_cfg['api_name']}/get_data', methods=['GET'])
def get_data(): def get_data():
return jsonify(data_dict) return jsonify(data_dict)
@app.route('/api/update_data', methods=['POST']) @app.route(f'/{server_cfg['api_name']}/update_data', methods=['POST'])
def receive_data(): def receive_data():
data = request.json data = request.json
# 如果存在对应标题则更新记录 # 如果存在对应标题则更新记录
@ -46,15 +54,6 @@ def init():
data_dict['server_dict'][server_name] = None data_dict['server_dict'][server_name] = None
def main(): def main():
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', default='server_config.json', type=str, help='the path of config json.')
args = parser.parse_args()
# 加载配置文件
cfg_path = args.cfg
global server_cfg
with open(cfg_path, 'r') as f:
server_cfg = json.load(f)
init() init()
# flask # flask
app.run(debug=False, host=server_cfg['host'], port=server_cfg['port']) app.run(debug=False, host=server_cfg['host'], port=server_cfg['port'])

3
server_config.json

@ -4,5 +4,6 @@
"server_list":["76", "174", "233", "222"], "server_list":["76", "174", "233", "222"],
"note_dict":{ "note_dict":{
"174": "test note" "174": "test note"
} },
"api_name": "api"
} }

4
web/js/script.js

@ -1,6 +1,6 @@
// 请求服务器获取数据 // 请求服务器获取数据
function fetchData() { function fetchData() {
fetch('http://10.1.16.174:15002/api/get_data') fetch('http://10.1.16.174:15002/api/get_data') // http://mm.zjgsu.edu.cn/serverInfo-api/get_data
// 获取服务器和显卡数据 // 获取服务器和显卡数据
.then(response => response.json()) // 解析 JSON 响应 .then(response => response.json()) // 解析 JSON 响应
.then(data => { .then(data => {
@ -130,7 +130,7 @@ function displayServerData(data){
serverData.cpu['temperature_list'].forEach(function(v){ serverData.cpu['temperature_list'].forEach(function(v){
temperature_list_str += v + " ℃ "; temperature_list_str += v + " ℃ ";
}); });
cpuInfo.innerHTML = "<strong>CPU型号 : </strong>" + serverData.cpu['name'] + "<br>" + cpuInfo.innerHTML = "<strong>" + serverData.cpu['name'] + "</strong><br>" +
"<strong>温度 : </strong>" + temperature_list_str + "<br>" + "<strong>温度 : </strong>" + temperature_list_str + "<br>" +
"<strong>占用率 : </strong>" + serverData.cpu['core_avg_occupy'] + "%"; "<strong>占用率 : </strong>" + serverData.cpu['core_avg_occupy'] + "%";

Loading…
Cancel
Save