def network_io_counters(pernic=False): """Return network I/O statistics as a namedtuple including: - number of bytes sent - number of bytes received - number of packets sent - number of packets received If pernic is True return the same information for every network interface installed on the system as a dictionary with network interface names as the keys and the namedtuple described above as the values. """ from psutil._common import ntuple_net_iostat rawdict = _psplatform.network_io_counters() if pernic: for nic, fields in rawdict.iteritems(): rawdict[nic] = ntuple_net_iostat(*fields) return rawdict else: bytes_sent, bytes_recv, packets_sent, packets_recv = 0, 0, 0, 0 for _, fields in rawdict.iteritems(): bytes_sent += fields[0] bytes_recv += fields[1] packets_sent += fields[2] packets_recv += fields[3] return ntuple_net_iostat(bytes_sent, bytes_recv, packets_sent, packets_recv)
def network_io_counters(pernic=False): """Return network I/O statistics as a namedtuple including the following attributes: - bytes_sent: number of bytes sent - bytes_recv: number of bytes received - packets_sent: number of packets sent - packets_recv: number of packets received - errin: total number of errors while receiving - errout: total number of errors while sending - dropin: total number of incoming packets which were dropped - dropout: total number of outgoing packets which were dropped (always 0 on OSX and BSD) If pernic is True return the same information for every network interface installed on the system as a dictionary with network interface names as the keys and the namedtuple described above as the values. """ rawdict = _psplatform.network_io_counters() if not rawdict: raise RuntimeError("couldn't find any network interface") if pernic: for nic, fields in rawdict.items(): rawdict[nic] = _nt_net_iostat(*fields) return rawdict else: return _nt_net_iostat(*[sum(x) for x in zip(*rawdict.values())])