Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. def get_bandwidth():
  2. # Get net in/out
  3. net1_out = psutil.net_io_counters().bytes_sent
  4. net1_in = psutil.net_io_counters().bytes_recv
  5.  
  6. time.sleep(300) # Not best way to handle getting a value 300 seconds later
  7.  
  8. # Get new net in/out
  9. net2_out = psutil.net_io_counters().bytes_sent
  10. net2_in = psutil.net_io_counters().bytes_recv
  11.  
  12. # Compare and get current speed
  13. if net1_in > net2_in:
  14. current_in = 0
  15. else:
  16. current_in = net2_in - net1_in
  17.  
  18. if net1_out > net2_out:
  19. current_out = 0
  20. else:
  21. current_out = net2_out - net1_out
  22.  
  23. network = {"traffic_in": current_in, "traffic_out": current_out}
  24.  
  25. # Return data in bytes
  26. return network
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement