Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to obtain current bandwidth usage?

Tags:

python

I need to get current bandwidth usage on given interface in my Python code. How can I achieve this?

I tried to extract the value from /proc/net/dev twice with short sleep interval (e.g. 0.3s) between two consecutive calls and compute the difference in bytes divided by time. It works but I do not know if I can trust this results. I'm looking for some more elegant solutions. Any tools, libraries, or simple algorithms?

I will appreciate all suggestions!

like image 958
Grzes Avatar asked Sep 25 '26 15:09

Grzes


1 Answers

There are libraries for this, like PSutil, but if you are on *Nix I would opt for using the underlying system.

Although you could use /proc/net/dev, rather use /sys/class/net/eth0/statistics since you will not have to parse anything since there are individual files for each statistic

like image 118
Leon Avatar answered Sep 27 '26 06:09

Leon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!