79
79
tot_after = psutil.network_io_counters()
80
80
pnic_after = psutil.network_io_counters(pernic=True)
81
81
return (tot_before, tot_after, pnic_before, pnic_after)
84
84
def refresh_window(tot_before, tot_after, pnic_before, pnic_after):
85
85
"""Print stats on screen."""
89
89
print_line("total bytes: sent: %-10s received: %s" \
90
90
% (bytes2human(tot_after.bytes_sent),
91
91
bytes2human(tot_after.bytes_recv))
93
93
print_line("total packets: sent: %-10s received: %s" \
94
94
% (tot_after.packets_sent, tot_after.packets_recv)
97
# per network interface
98
# per-network interface details: let's sort network interfaces so
99
# that the ones which generated more traffic are shown first
99
for nic in pnic_after:
100
stats_before = pnic_before[nic]
101
stats_after = pnic_after[nic]
101
nic_names = pnic_after.keys()
102
nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
103
for name in nic_names:
104
stats_before = pnic_before[name]
105
stats_after = pnic_after[name]
102
106
templ = "%-15s %15s %15s"
104
nic, "TOTAL", "PER-SEC"),
107
print_line(templ % (name, "TOTAL", "PER-SEC"), highlight=True)
107
108
print_line(templ % (
109
110
bytes2human(stats_after.bytes_sent),