-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetspeed.py
More file actions
executable file
·312 lines (242 loc) · 7.95 KB
/
netspeed.py
File metadata and controls
executable file
·312 lines (242 loc) · 7.95 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#! /usr/bin/python3
#! encoding:utf-8
'''
很早之前写的python软件,代码不好看
操作方法:
左键双击窗口换肤,右键双击退出程序;
右键单击窗口左侧切换显示流量还是CPU和内存占用;
右键单击窗口右侧切换显示全部或一部分内容。
悬浮窗口移动到屏幕右边时会自动隐藏,当鼠标再次移动到悬浮窗口上时,悬浮窗口会弹出。
09版更新:增加CPU和内存占用显示
'''
import time
import datetime
import os
import os.path
import sys
import tkinter as tk
import json
from subprocess import check_output
DEFAULT_VERSION = 0.9
DEFAULT_POSx = 50 # 窗口默认起始位置
DEFAULT_POSy = 50
DEMO_CPU = 'CPU:{:.0f}% MEM:{:.0f}%'
DEMO_NET = '↓ {:<5} ↑ {:>5}'
SKIN_LIST = (('GreenYellow', 'black'), ('#F5BB00', 'white'),
('DeepSkyBlue', 'Ivory'), ('Violet', 'Ivory'))
# button binding
PROG_CATCH = '<Button-1>'
PROG_MOVE = '<B1-Motion>'
PROG_EXIT = '<Double-Button-3>'
PROG_SKIN_CG = '<Button-3>'
PROG_MODE_CG = '<Double-Button-1>'
PROG_SHOW = '<Enter>'
# config file path
CONFIG_FILE_PATH = sys.path[0] + '/config.json'
LOCK_FILE_PATH = sys.path[0] + '/netspeed.loc'
LAUNCHER_ICON_PATH = sys.path[0] + '/Minimalist.png'
LAUNCHER_FILE_PATH = os.environ['HOME'] + \
'/.local/share/applications/Minimalist.desktop'
DEMO_LAUNCHER = '''
[Desktop Entry]
Encoding=UTF-8
Name=Minimalist
Comment=极简系统监视器
Exec=python3 {}
Icon={}
Categories=Application;
Version={}
Type=Application
Terminal=false
'''
class SoftConfig():
''' 配置信息 '''
# 是否显示为单列模式(弃用)
IsSingleMode = False
# 显示网速或者cpu,内存数据
IsShowNet = True
# 程序位置
CurPos = [DEFAULT_POSx, DEFAULT_POSy]
# 程序版本
Version = DEFAULT_VERSION
# 皮肤样式
CurSkin = 0
class SoftData():
''' 运行非保存数据 '''
IsNetFirstRun = True
IsCpuFirstRun = True
Time = 0
CpuData = []
RecvList = []
SendList = []
c = SoftConfig()
d = SoftData()
root = tk.Tk()
mainUI = tk.Label()
def func(x, y):
return int(y) - int(x)
def humanize(netSpeed):
'''人性化显示流量单位'''
n = 0
while netSpeed >= 1000:
netSpeed /= 1024
n += 1
return '{:.1f}'.format(netSpeed) + ('B', 'K', 'M', 'G', 'T')[n]
def get_cpu_and_mem_date():
''' 获取cpu、内存占用数据 '''
if d.IsNetFirstRun:
d.IsNetFirstRun = False
d.Time = time.time()
d.CpuData = open('/proc/stat', 'r').readline().split()[1:]
return DEMO_CPU.format(0, 0)
curTime = time.time()
curCpuData = open('/proc/stat', 'r').readline().split()[1:]
cpuRate = 1 - (int(curCpuData[3]) - int(d.CpuData[3])) / \
sum(map(func, d.CpuData, curCpuData))
# 避免由于计算误差出现使用率超过100%的情况
if cpuRate > 1.0:
cpuRate = 1.0
d.CpuData = curCpuData
d.Time = curTime
memData = open('/proc/meminfo', 'r').readlines()
memRate = 1 - int(memData[2].split()[1]) / int(memData[0].split()[1])
return DEMO_CPU.format(cpuRate * 100, memRate * 100)
def get_net_date():
''' 获取网络数据 '''
curRecvList = []
curSendList = []
if d.IsCpuFirstRun:
d.IsCpuFirstRun = False
d.RecvList = []
d.SendList = []
dates = open('/proc/net/dev', 'r').readlines()[2:]
d.Time = time.time()
for date in dates:
d.RecvList.append(int(date.split()[1]))
d.SendList.append(int(date.split()[9]))
time.sleep(0.5)
dates = open('/proc/net/dev', 'r').readlines()[2:]
curTime = time.time()
for date in dates:
curRecvList.append(int(date.split()[1]))
curSendList.append(int(date.split()[9]))
recv = max(map(func, d.RecvList, curRecvList)) / (curTime - d.Time)
send = max(map(func, d.SendList, curSendList)) / (curTime - d.Time)
d.RecvList = curRecvList.copy()
d.SendList = curSendList.copy()
d.Time = curTime
return DEMO_NET.format(humanize(recv), humanize(send))
def refresh():
'''实时刷新流量显示'''
if c.IsShowNet:
mainUI.config(text=get_net_date(), width=20)
else:
mainUI.config(text=get_cpu_and_mem_date(), width=20)
root.after(1000, refresh)
def mouse_move(e):
'''移动窗口'''
if e.x_root < root.winfo_screenwidth() - 10:
relPos = [e.x - c.CurPos[0] + root.winfo_x(),
e.y - c.CurPos[1] + root.winfo_y()]
if relPos[0] < 10:
relPos[0] = 0
if relPos[0] > root.winfo_screenwidth() - root.winfo_width() - 10:
relPos[0] = root.winfo_screenwidth() - 4
if relPos[1] < 10:
relPos[1] = 0
if relPos[1] > root.winfo_screenheight() - root.winfo_height() - 10:
relPos[1] = root.winfo_screenheight() - root.winfo_height()
root.geometry('+{}+{}'.format(relPos[0], relPos[1]))
def mouse_click(e):
'''左键单击窗口时获得鼠标位置,辅助移动窗口'''
c.CurPos = [e.x, e.y]
def change_skin(e):
''' 修改皮肤 '''
if c.CurSkin == len(SKIN_LIST)-1:
c.CurSkin = 0
else:
c.CurSkin += 1
mainUI.config(bg=SKIN_LIST[c.CurSkin][0], fg=SKIN_LIST[c.CurSkin][1])
def change_mode(e):
'''切换显示内容'''
global c
c.IsShowNet = not c.IsShowNet
def exit_program(e):
'''退出程序,并保存退出之前状态'''
writeConfig()
deinit()
root.destroy()
def show(e):
'''取消隐藏'''
if root.winfo_x() == root.winfo_screenwidth() - 4:
root.geometry('+{}+{}'.format(root.winfo_screenwidth() -
root.winfo_width() - 10, root.winfo_y()))
def writeConfig():
c.CurPos = [root.winfo_x(), root.winfo_y()]
dic = {
'isSingleMode': c.IsSingleMode,
'isShowNet': c.IsShowNet,
'curPos': c.CurPos,
'version': c.Version,
'curSkin': c.CurSkin
}
with open(CONFIG_FILE_PATH, 'w') as f:
json.dump(dic, f)
def readConfig():
# 判断是否安装过该软件,如果没有,就开始安装
if os.path.exists(CONFIG_FILE_PATH):
with open(CONFIG_FILE_PATH, 'r') as f:
dic = json.load(f)
c.IsSingleMode = dic['isSingleMode']
c.IsShowNet = dic['isShowNet']
c.CurPos = dic['curPos']
c.Version = dic['version']
c.CurSkin = dic['curSkin']
# else:
# # 启动器图标所需内容
# content = DESKTOP_ENTRY_DEMO.format(
# os.path.realpath(__file__), LAUNCHER_ICON, version)
# with open(LAUNCHER_FILE_PATH) as f:
# f.write(content)
def init():
''' 初始化 '''
# 判断是否已经运行了一个实例
if os.path.exists(LOCK_FILE_PATH) == True:
with open(LOCK_FILE_PATH, 'r') as f:
old_pid = f.readline()
with os.popen('ps '+old_pid) as p:
proc = p.read()
if 'netspeed.py' in proc:
os.system('kill '+old_pid)
# return False
with open(LOCK_FILE_PATH, 'w') as f:
f.write(str(os.getpid()))
return True
def deinit():
''' 清理现场 '''
if os.path.exists(LOCK_FILE_PATH):
os.remove(LOCK_FILE_PATH)
def main():
global mainUI
global root
global c
global d
if init() == False:
return
readConfig()
root.geometry('+{}+{}'.format(c.CurPos[0], c.CurPos[1])) # 窗口初始位置
root.overrideredirect(True) # 去掉标题栏
root.wm_attributes('-topmost', 1) # 置顶窗口
mainUI = tk.Label(root, text=' starting... ',
bg=SKIN_LIST[c.CurSkin][0], fg=SKIN_LIST[c.CurSkin][1])
mainUI.pack()
mainUI.bind(PROG_CATCH, mouse_click)
mainUI.bind(PROG_MOVE, mouse_move)
mainUI.bind(PROG_EXIT, exit_program)
# mainUI.bind(PROG_SKIN_CG, change_skin)
mainUI.bind(PROG_MODE_CG, change_mode)
mainUI.bind(PROG_SHOW, show)
root.after(1000, refresh)
root.mainloop()
if __name__ == '__main__':
main()