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 argparse
import requests import requests
import subprocess import subprocess
from version import version
# region get data # region get data
@ -190,6 +191,7 @@ def collect_data():
result_dict['note'] = client_cfg['note'] result_dict['note'] = client_cfg['note']
result_dict['title'] = client_cfg['title'] result_dict['title'] = client_cfg['title']
result_dict['interval'] = client_cfg['interval'] result_dict['interval'] = client_cfg['interval']
result_dict['version'] = version
return result_dict return result_dict
@ -205,11 +207,12 @@ def main():
# 持续发送 # 持续发送
send_interval = client_cfg['interval'] 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: while True:
data = collect_data() data = collect_data()
try: try:
requests.post(api_url, json=data) result = requests.post(api_url, json=data)
except Exception as e: except Exception as e:
print(e) print(e)
time.sleep(send_interval) 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", "title": "174",
"interval": 3.0, "interval": 3.0,
"note": "", "note": "",

9
server.py

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

Loading…
Cancel
Save