Build an Internet Speedmeter in Python

In this article, we are going to build an internet speedmeter utility using Python. Not to be confused with the speed test, which tests your internet connection. Speedmeter tracks the current bandwidth.

The approach is simple. Every os keeps track of its bandwidth usage.

  • Linux

    -> cat /proc/net/dev
    

    Output

    Inter-|   Receive                                                |  Transmit
     face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo 
    colls carrier compressed
    dummy0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0         0
     bond0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0         0
      sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0         0
        lo:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0         0
      eth0:  626546     477    0    0    0     0          0        12    23521     290    0    0    0     0       0         0
    
  • Windows

    Get-NetAdapterStatistics
    
    Name                             ReceivedBytes ReceivedUnicastPackets       SentBytes SentUnicastPackets
    ----                             ------------- ----------------------       --------- ------------------
    Wi-Fi                               1161133642                 838629        39381324             267615
    Ethernet                                     0                      0               0                  0
    vEthernet (WSL)                       56507952                  84617       464558916             315009
    

For parsing these data, we can use the psutil package.

Function to get the current bytes count

from psutil import net_io_counters

def get_data():
    network = net_io_counters()
    return network.bytes_recv, network.bytes_sent

Steps to perform

  • Get old bytes count
  • Get the new bytes count
  • Find the difference with old ones
  • print(difference/1024)
  • Update the old bytes count with new ones
  • wait 1 sec
import time

recvInit, sendInit = get_data()

try:
    while True:
        recvNew, sendNew = get_data()
        print(
            f"\rDownload: {(recvNew - recvInit)/1024:.2f} KBps, Upload: {(sendNew-sendInit)/1024:.2f} KBps",
            flush=True,
            end=" ",
        )
        recvInit, sendInit = recvNew, sendNew
        time.sleep(1)
except KeyboardInterrupt as e:
    print("\nQuitting...")

Since the time taken to read is not accounted for, there might be a slight difference in the calculation of speed.

Demo

speedmeter.gif

This works on both Windows and Linux.

Go version of the same utility can be found here(currently not stable). The go version works on systray instead of printing to stdout.

speedmeter-systray.jpg

Happy Hacking✨✨

Article cover by Christian Englmeier

No Comments Yet