Skip to content

Instantly share code, notes, and snippets.

@tapionx
Created March 29, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tapionx/89e120a55f1f8ff604b75c988781d078 to your computer and use it in GitHub Desktop.
Save tapionx/89e120a55f1f8ff604b75c988781d078 to your computer and use it in GitHub Desktop.
import os
import time
def get_total_bytes(interfaces):
rx_bytes = 0
tx_bytes = 0
for interface in interfaces:
with open('/sys/class/net/{}/statistics/rx_bytes'.format(interface)) as f:
rx_bytes += int(f.read())
with open('/sys/class/net/{}/statistics/tx_bytes'.format(interface)) as f:
tx_bytes += int(f.read())
return (rx_bytes, tx_bytes)
interfaces = os.listdir('/sys/class/net/')
if 'lo' in interfaces:
interfaces.remove('lo')
(rx_bytes1, tx_bytes1) = get_total_bytes(interfaces)
time.sleep(1)
(rx_bytes2, tx_bytes2) = get_total_bytes(interfaces)
rx_bitrate = ((rx_bytes2 - rx_bytes1) / 1024 ) * 8
tx_bitrate = ((tx_bytes2 - tx_bytes1) / 1024 ) * 8
print(rx_bitrate, tx_bitrate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment