Average network statistics in Python
<p>I'm new to python and I need to calculate the 'average speed' of my network interface(like the tool nload) on a linux machine. The below code is telling me how much bytes have been sent and received. I would appreciate if someone could help me print the average speed of my network interface. </p>

<pre><code>def main():
    rx_bytes, tx_bytes = get_network_bytes('eth0')
    print '%i bytes received' % rx_bytes
    print '%i bytes sent' % tx_bytes

def get_network_bytes(interface):
    for line in open('/proc/net/dev', 'r'):
        if interface in line:
            data = line.split('%s:' % interface)[1].split()
            rx_bytes, tx_bytes = (data[0], data[8])
            return (int(rx_bytes), int(tx_bytes))

if __name__ == '__main__':
    main()
</code></pre>
