John Pydev
John Pydev

Reputation: 21

How to get bandwidth from eth0 use python script?

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

$python script.py eth0
UP: 5 KB/sec
DOWN: 30.5 KB/sec

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

Upvotes: 1

Views: 524

Answers (1)

Rigas Papas
Rigas Papas

Reputation: 11

You can use Psutil to get information about network interfaces like this:

psutil.net_io_counters(pernic=True)

This will return something like the following

{'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)}

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 this.

Upvotes: 1

Related Questions