forked from Pure-Peace/system-info-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
252 lines (199 loc) · 5.88 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'''
@author: Pure-Peace
@name: 系统信息
@time: 2020年8月17日
@version: 0.1
'''
import systemInfo
from utils import log, getTime
from gevent import monkey; monkey.patch_all()
import threading
import time
import json
from flask_socketio import SocketIO
from flask import Flask, jsonify, request
from flask_cors import CORS
# fix: windows cmd cannot display colors
from colorama import init
init(autoreset=True)
# initial(s)
app = Flask(__name__)
#app.config['SECRET_KEY'] = 'asdqwezxc'
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
app.config["JSON_AS_ASCII"] = False
# 跨域
CORS(app)
socketio = SocketIO()
socketio.init_app(app, cors_allowed_origins='*', async_mode='gevent')
class Cache:
def __init__(self, name: str, limit: int = 30):
'''
缓存
Parameters
----------
name : str
缓存名称,关系到文件保存.
limit : int, optional
缓存上限,最高存储多少条信息. The default is 30.
Returns
-------
None.
'''
self.name = name
self.limit = limit
self.read()
def add(self, item):
'''
添加数据到缓存中
Parameters
----------
item : TYPE
DESCRIPTION.
Returns
-------
None.
'''
if len(self.data) >= self.limit: del(self.data[0])
self.data.append(item)
self.save()
def save(self):
'''
将缓存持久化,存储到name对应的json文件中
Returns
-------
None.
'''
try:
with open(f'{self.name}.json', 'w', encoding='utf-8') as file:
json.dump(self.data, file, indent=4)
except:
pass
def read(self):
'''
读取缓存(如果有)
Returns
-------
None.
'''
try:
with open(f'{self.name}.json', 'r', encoding='utf-8') as file:
self.data = json.load(file)
except:
self.data = []
# apis -------------------------------
@app.route('/')
def root():
return jsonify({'status': 1, 'message': 'hello'})
@app.route('/cpu_constants')
def cpuConstants():
return jsonify(cpuData)
@app.route('/cpu_info')
def cpuInfo():
return jsonify(cpuCache.data)
@app.route('/io_info')
def ioInfo():
return jsonify(ioCache.data)
@app.route('/mem_info')
def memInfo():
return jsonify(memCache.data)
@app.route('/network_info')
def networkInfo():
return jsonify(networkCache.data)
@app.route('/load_info')
def loadInfo():
return jsonify(loadCache.data)
# socketio -------------------------------
@socketio.on('disconnect')
def sioDisconnect():
log('socketio连接断开:', request.remote_addr)
@socketio.on('connect')
def sioConnect():
log('socketio连接建立:', request.remote_addr)
# 背景线程 -------------------------------
def cpuBackground(interval: int = 8) -> None:
'''
更新cpu信息并更新缓存,同时向前端socketio推送信息
Parameters
----------
interval : int, optional
间隔多少时间更新一次cpu信息. The default is 8.
Returns
-------
None
DESCRIPTION.
'''
def task():
data: dict = systemInfo.GetCpuInfo(constants = False) # 获取cpu信息
cpuCache.add(data) # 更新缓存
socketio.emit('update_cpu', data, broadcast=True) # 推送信息,事件名为update_cpu
loopRun(task, interval) # 循环执行,每隔interval秒执行一次
def ioBackground(interval: int = 5) -> None:
def task():
data: dict = systemInfo.GetIoReadWrite()
ioCache.add(data)
socketio.emit('update_io', data, broadcast=True)
loopRun(task, interval)
def memBackground(interval: int = 5) -> None:
def task():
data: dict = systemInfo.GetMemInfo()
memCache.add(data)
socketio.emit('update_mem', data, broadcast=True)
loopRun(task, interval)
def networkBackground(interval: int = 5) -> None:
def task():
data: dict = systemInfo.GetNetWork()
networkCache.add(data)
socketio.emit('update_net', data, broadcast=True)
loopRun(task, interval)
def loadBackground(interval: int = 5) -> None:
def task():
data: dict = systemInfo.GetLoadAverage()
loadCache.add(data)
socketio.emit('update_load', data, broadcast=True)
loopRun(task, interval)
def loopRun(func, interval: int, *arg, **kwargs) -> None:
'''
循环执行
Parameters
----------
func : TYPE
要执行的函数.
interval : int
间隔时间.
*arg : TYPE
位置参数.
**kwargs : TYPE
关键字参数.
Returns
-------
None
DESCRIPTION.
'''
while True:
try:
time.sleep(interval)
func(*arg, **kwargs)
except Exception as err:
log('循环线程执行异常:', err)
# 线程列表
ts: list = [
threading.Thread(target=cpuBackground),
threading.Thread(target=ioBackground),
threading.Thread(target=memBackground),
threading.Thread(target=networkBackground),
threading.Thread(target=loadBackground)
]
# 获取cpu常量信息
cpuData = systemInfo.cpuConstants.getDict
# 建立缓存
cpuCache = Cache('cpuInfo')
ioCache = Cache('ioInfo')
memCache = Cache('memInfo')
networkCache = Cache('networkInfo')
loadCache = Cache('loadInfo')
if __name__ == '__main__':
# 开启所有线程
for t in ts: t.start()
log('应用已启动')
# 启动app(危险:0.0.0.0将使得外网可直接访问)
socketio.run(app, host = '0.0.0.0', port = 5678)