63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
# coding: utf-8
|
|
|
|
try:
|
|
import psutil
|
|
except ImportError:
|
|
print('Error: psutil module not found!')
|
|
exit()
|
|
|
|
|
|
def get_key():
|
|
|
|
key_info = psutil.net_io_counters(pernic=True).keys()
|
|
#print(key_info)
|
|
|
|
recv = {}
|
|
sent = {}
|
|
print("--------------------------------------------")
|
|
print(psutil.net_io_counters(pernic=True).get("eth0"))
|
|
print("--------------------------------------------")
|
|
|
|
for key in key_info:
|
|
print(key)
|
|
print(psutil.net_io_counters())
|
|
recv.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_recv)
|
|
sent.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_sent)
|
|
|
|
return key_info, recv, sent
|
|
|
|
|
|
def get_rate(func):
|
|
|
|
import time
|
|
|
|
key_info, old_recv, old_sent = func()
|
|
|
|
time.sleep(1)
|
|
|
|
key_info, now_recv, now_sent = func()
|
|
|
|
net_in = {}
|
|
net_out = {}
|
|
|
|
for key in key_info:
|
|
net_in.setdefault(key, (now_recv.get(key) - old_recv.get(key)) / 1024)
|
|
net_out.setdefault(key, (now_sent.get(key) - old_sent.get(key)) / 1024)
|
|
|
|
return key_info, net_in, net_out
|
|
|
|
|
|
def traffic_show():
|
|
try:
|
|
key_info, net_in, net_out = get_rate(get_key)
|
|
|
|
for key in key_info:
|
|
print('%s\nInput:\t %-5sKB/s\nOutput:\t %-5sKB/s\n' % (key, net_in.get(key), net_out.get(key)))
|
|
except KeyboardInterrupt:
|
|
exit()
|
|
|
|
if __name__ == '__main__':
|
|
#traffic_show()
|
|
key_info = psutil.net_io_counters().keys()
|
|
print("#### %s" %(key_info))
|