


# global boolean for status of the programĪfter we import the necessary libraries, we initialize our global variables that will be used in our upcoming functions: # the global Pandas DataFrame that's used to track previous traffic stats # A dictionary to map each process ID (PID) to total Upload (0) and Download (1) traffic # A dictionary to map each connection to its correponding process ID (PID) # print the total download/upload along with current speeds Us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv Now let's enter the loop that gets the same stats but after a delay so we can calculate the download and upload speed: while True: # extract the total bytes sent and receivedīytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv Next, we will use _io_counters() function that returns the network input and output statistics: # get the network I/O stats from psutil Starting with the simplest program Let's import psutil and make a function that prints the bytes in a nice format: import psutilįor unit in : Psutil is a cross-platform library for retrieving information on running processes and system and hardware information in Python, we will be using it for retrieving network statistics as well as established connections. To get started, let's install the required libraries: $ pip install psutil scapy pandas Have you ever wanted to make a program that monitors the network usage of your machine? In this tutorial, we will make three Python scripts that monitor total network usage, network usage per network interface, and network usage per system process:
