lxb 7 months ago
parent
commit
ab4c849d21
  1. 7
      client.py
  2. 2
      client_config.json
  3. 9
      server.py
  4. 1
      version.py
  5. 2
      web/js/script.js

7
client.py

@ -5,6 +5,7 @@ import psutil
import argparse
import requests
import subprocess
from version import version
# region get data
@ -190,6 +191,7 @@ def collect_data():
result_dict['note'] = client_cfg['note']
result_dict['title'] = client_cfg['title']
result_dict['interval'] = client_cfg['interval']
result_dict['version'] = version
return result_dict
@ -205,11 +207,12 @@ def main():
# 持续发送
send_interval = client_cfg['interval']
api_url = client_cfg['server_url'] + f'/{client_cfg['api_name']}/update_data'
api_name = client_cfg['api_name']
api_url = client_cfg['server_url'] + f'/{api_name}/update_data'
while True:
data = collect_data()
try:
requests.post(api_url, json=data)
result = requests.post(api_url, json=data)
except Exception as e:
print(e)
time.sleep(send_interval)

2
client_config.json

@ -1,5 +1,5 @@
{
"server_url": "http://127.0.0.1:15002",
"server_url": "http://10.1.16.174:15001",
"title": "174",
"interval": 3.0,
"note": "",

9
server.py

@ -1,5 +1,6 @@
from flask import Flask, jsonify, request
from flask_cors import CORS
from version import version
import json
import argparse
@ -17,21 +18,22 @@ args = parser.parse_args()
cfg_path = args.cfg
with open(cfg_path, 'r') as f:
server_cfg = json.load(f)
api_name = server_cfg['api_name']
#endregion
#region 接口
# 测试用
@app.route(f'/{server_cfg['api_name']}')
@app.route(f'/{api_name}')
def hello():
return 'hi. —— CheckGPUsWeb'
@app.route(f'/{server_cfg['api_name']}/get_data', methods=['GET'])
@app.route(f'/{api_name}/get_data', methods=['GET'])
def get_data():
return jsonify(data_dict)
@app.route(f'/{server_cfg['api_name']}/update_data', methods=['POST'])
@app.route(f'/{api_name}/update_data', methods=['POST'])
def receive_data():
data = request.json
# 如果存在对应标题则更新记录
@ -50,6 +52,7 @@ def receive_data():
def init():
data_dict['server_dict'] = dict()
data_dict['version'] = version
for server_name in server_cfg['server_list']:
data_dict['server_dict'][server_name] = None

1
version.py

@ -0,0 +1 @@
version = "0.1.0.20241209_beta"

2
web/js/script.js

@ -1,6 +1,6 @@
// 请求服务器获取数据
function fetchData() {
fetch('http://10.1.16.174:15002/api/get_data') // http://mm.zjgsu.edu.cn/serverInfo-api/get_data
fetch('http://10.1.16.174:15001/api/get_data') // http://mm.zjgsu.edu.cn/serverInfo-api/get_data
// 获取服务器和显卡数据
.then(response => response.json()) // 解析 JSON 响应
.then(data => {

Loading…
Cancel
Save