3

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!

frlan
  • 6,950
  • 3
  • 31
  • 72
Grzes
  • 971
  • 1
  • 13
  • 28
  • 2
    you mean like this? http://stackoverflow.com/questions/26573681/python-getting-upload-download-speeds/26853086#26853086 – Leon May 04 '15 at 13:33

1 Answers1

4

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

Leon
  • 12,013
  • 5
  • 36
  • 59