I would recommend using the psutil.network_io_counters
module:
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.
In [1]: import psutil
In [2]: psutil.network_io_counters(pernic=True)
Out[2]:
{'en0': iostat(bytes_sent=143391435L, bytes_recv=1541801914L,
packets_sent=827983L, packets_recv=1234558L, errin=0L, errout=0L,
dropin=0L, dropout=0),
'gif0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
errin=0L, errout=0L, dropin=0L, dropout=0),
'lo0': iostat(bytes_sent=6143860L, bytes_recv=6143860L, packets_sent=55671L,
packets_recv=55671L, errin=0L, errout=0L, dropin=0L, dropout=0),
'p2p0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
errin=0L, errout=0L, dropin=0L, dropout=0),
'stf0': iostat(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L,
errin=0L, errout=0L, dropin=0L, dropout=0)
}
You can keep track of the sent and received bytes on the interface you want to monitor and that will give you information if the network is idle or busy.
ifconfig eth0
which would give you rx and tx bytes and if run at regular intervals could give you a simple metric. I would wrap the command in pexpect (or similar) and examine the output. However I too would be interested in a proper way to do this. – Verma Aug 1 at 3:43