Qilan2 commited on
Commit
6dc418f
·
verified ·
1 Parent(s): f6162b4

Update sbx/b.py

Browse files
Files changed (1) hide show
  1. sbx/b.py +100 -2
sbx/b.py CHANGED
@@ -1,5 +1,6 @@
1
- from flask import Flask, request, jsonify
2
  import os
 
3
 
4
  app = Flask(__name__)
5
 
@@ -35,5 +36,102 @@ def get_sub_content():
35
  except Exception as e:
36
  return jsonify({"error": str(e)}), 500
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  if __name__ == '__main__':
39
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ from flask import Flask, request, jsonify, Response
2
  import os
3
+ import time
4
 
5
  app = Flask(__name__)
6
 
 
36
  except Exception as e:
37
  return jsonify({"error": str(e)}), 500
38
 
39
+ @app.route('/xx', methods=['GET'])
40
+ def system_info():
41
+ try:
42
+ # 获取CPU信息
43
+ cpu_cores = os.cpu_count()
44
+
45
+ # 获取CPU使用率 - 使用/proc/stat计算
46
+ def get_cpu_usage():
47
+ with open('/proc/stat', 'r') as f:
48
+ lines = f.readlines()
49
+ for line in lines:
50
+ if line.startswith('cpu '):
51
+ fields = line.split()
52
+ total_time = sum(int(x) for x in fields[1:])
53
+ idle_time = int(fields[4])
54
+ return total_time, idle_time
55
+ return 0, 0
56
+
57
+ # 第一次读取
58
+ total1, idle1 = get_cpu_usage()
59
+ time.sleep(0.1) # 短暂延迟
60
+ # 第二次读取
61
+ total2, idle2 = get_cpu_usage()
62
+
63
+ if total2 - total1 > 0:
64
+ cpu_usage = 100 * (1 - (idle2 - idle1) / (total2 - total1))
65
+ else:
66
+ cpu_usage = 0
67
+
68
+ # 获取内存信息
69
+ with open('/proc/meminfo', 'r') as f:
70
+ mem_lines = f.readlines()
71
+
72
+ mem_total = 0
73
+ mem_available = 0
74
+ mem_free = 0
75
+
76
+ for line in mem_lines:
77
+ if line.startswith('MemTotal:'):
78
+ mem_total = int(line.split()[1])
79
+ elif line.startswith('MemAvailable:'):
80
+ mem_available = int(line.split()[1])
81
+ elif line.startswith('MemFree:'):
82
+ mem_free = int(line.split()[1])
83
+
84
+ mem_used = mem_total - mem_available if mem_available > 0 else mem_total - mem_free
85
+ mem_usage_percent = 100 * mem_used / mem_total if mem_total > 0 else 0
86
+
87
+ # 转换为GB
88
+ mem_total_gb = round(mem_total / 1024 / 1024, 2)
89
+ mem_used_gb = round(mem_used / 1024 / 1024, 2)
90
+ mem_free_gb = round(mem_free / 1024 / 1024, 2)
91
+
92
+ # 获取系统运行时间
93
+ with open('/proc/uptime', 'r') as f:
94
+ uptime_seconds = float(f.readline().split()[0])
95
+
96
+ # 格式化运行时间
97
+ uptime_days = int(uptime_seconds // 86400)
98
+ uptime_hours = int((uptime_seconds % 86400) // 3600)
99
+ uptime_minutes = int((uptime_seconds % 3600) // 60)
100
+
101
+ # 获取负载平均值
102
+ with open('/proc/loadavg', 'r') as f:
103
+ load_avg = f.readline().split()[0:3]
104
+
105
+ # 构建格式化的中文响应
106
+ response_text = f"""
107
+ ============ 系统信息 ============
108
+
109
+ CPU信息:
110
+ 核心数: {cpu_cores} 核
111
+ 使用率: {cpu_usage:.2f}%
112
+
113
+ 内存信息:
114
+ 总内存: {mem_total_gb} GB
115
+ 已使用: {mem_used_gb} GB
116
+ 空闲内存: {mem_free_gb} GB
117
+ 使用率: {mem_usage_percent:.2f}%
118
+
119
+ 系统运行时间:
120
+ {uptime_days}天 {uptime_hours}小时 {uptime_minutes}分钟
121
+
122
+ 系统负载:
123
+ 1分钟平均: {load_avg[0]}
124
+ 5分钟平均: {load_avg[1]}
125
+ 15分钟平均: {load_avg[2]}
126
+
127
+ ==================================
128
+ """
129
+
130
+ return Response(response_text, mimetype='text/plain; charset=utf-8')
131
+
132
+ except Exception as e:
133
+ error_text = f"获取系统信息时出错: {str(e)}"
134
+ return Response(error_text, mimetype='text/plain; charset=utf-8'), 500
135
+
136
  if __name__ == '__main__':
137
+ app.run(host='0.0.0.0', port=7860, debug=True)