-
Notifications
You must be signed in to change notification settings - Fork 0
/
netspeed.py
63 lines (50 loc) · 1.51 KB
/
netspeed.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
import os
import time
import sys
def get_download_bytes(line) :
bytes_split = line.split(' ')
while '' in bytes_split :
bytes_split.remove('')
downloadBytes = bytes_split[4]
# print(downloadBytes)
return downloadBytes
def get_bytes(text, net_card):
start = False
for line in text:
if net_card in line :
start = True
if start :
if 'RX packets' in line :
download_bytes = get_download_bytes(line)
start = False
return int(download_bytes)
return 0
def calculate_speed(net_cards) :
while 1 :
text = os.popen('ifconfig').readlines()
old_bytes = 0
for net_card in net_cards:
old_bytes += get_bytes(text, net_card)
# print(old_bytes)
time.sleep(0.5)
text = os.popen('ifconfig').readlines()
new_bytes = 0
for net_card in net_cards:
new_bytes += get_bytes(text, net_card)
# print(new_bytes)
speed = round((new_bytes - old_bytes) / 1024, 2)
# print(speed)
sys.stdout.write(' ' * 20 + '\r')
sys.stdout.flush()
sys.stdout.write('Speed: {} Kb/s \r'.format(speed))
def netSpeed():
text = os.popen('ifconfig').readlines()
net_cards = []
# find all net cards
for line in text:
if 'flags=' in line:
tmp = line[0:line.index(': flags')]
net_cards.append(tmp)
calculate_speed(net_cards)
if __name__ == '__main__' :
netSpeed()