How to get bandwidth from eth0 use python script?
<p>Python in ubuntu ifconfig  : How to get bandwidth value from device eth0 use os modules ifconfig in python script, like this: </p>

<pre><code>$python script.py eth0
UP: 5 KB/sec
DOWN: 30.5 KB/sec
</code></pre>

<p>where output script can change every second time and value bandwidth use Kb/s
?</p>

<p>You can use <a href="https://pythonhosted.org/psutil/" rel="nofollow">Psutil</a> to get information about network interfaces like this:</p>

<pre><code>psutil.net_io_counters(pernic=True)
</code></pre>

<p>This will return something like the following</p>

<pre><code>{'awdl0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
 'bridge0': snetio(bytes_sent=342L, bytes_recv=0L, packets_sent=1L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
 'en0': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
 'en1': snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0),
 'en4': snetio(bytes_sent=68008896L, bytes_recv=1972984495L, packets_sent=776722L, packets_recv=1487084L, errin=0L, errout=10L, dropin=0L, dropout=0),
 'lo0': snetio(bytes_sent=87119711L, bytes_recv=87119711L, packets_sent=54606L, packets_recv=54606L, errin=0L, errout=0L, dropin=0L, dropout=0)}
</code></pre>

<p>You can measure the difference on sent/received bytes once every second and print the up/down speeds. If you want the speeds to be human readable take a look at how you at <a href="https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size">this</a>.</p>

python ubuntu module bandwidth