Create a Network Traffic Monitor using Python

it's a network traffic monitor using python psutil and prettytable library

Introduction

You are reading this article means your system has an internet connection. But do you ever check how much data your computer sent and received? There is a default application called Task Manager for Windows, and System Monitor for Linux and Mac which provides us with network information along with other system information.

In this tutorial, we will develop a Network Traffic Monitor using Python that will show network information, for example, the total amount of data sent and receive, and the speed of downloading and sending of packets.

Python offers a cross-platform library called psutil (process and system utilities) that can extract information about running processes and system utilization. We will use this magic tool here to retrieve Network Information using a Python Program. Here we will use another library called prettytable to display the result data in a nice tabular form.

👉Visit Also: Extract Text from an Image using a Python Program

Requirements

Since we are gonna use two third-party libraries here to get our desired result, you must have installed those in your system. The following steps will help you.

👉Install psutil: pip install psutil

👉Install prettytable: pip install prettytable

The Program

The entire program is pretty straightforward. I have added one comment line before each line of the code. You will get the logic easily once you see it.


import os
import time
import psutil
from prettytable import PrettyTable
from prettytable import DOUBLE_BORDER

# Units of memory sizes
size = ['bytes', 'KB', 'MB', 'GB', 'TB']

# Function that returns bytes in a readable format
def getSize(bytes):
for unit in size:
if bytes < 1024:
return f"{bytes:.1f}{unit}"
bytes /= 1024

# Prints the Data on the Terminal or Console
def printData():
# Creating an instance of PrettyTable class
card = PrettyTable()
card.set_style(DOUBLE_BORDER)
# Column Names of the table
card.field_names = ["Received", "Receiving", "Sent", 'Sending']
# Adding row to the table
card.add_row([f"{getSize(netStats2.bytes_recv)}", \
f"{getSize(downloadStat)}/s", f"{getSize(netStats2.bytes_sent)}", \
f"{getSize(uploadStat)}/s"])
print(card)

# psutil.net_io_counters() returns network I/O statistics as a namedtuple
netStats1 = psutil.net_io_counters()

# Getting the data of total bytes sent and received
dataSent = netStats1.bytes_sent
dataRecv = netStats1.bytes_recv

# Running a loop to get the data continuously
while True:
# Delay for one second
time.sleep(1)

# Clear the Terminal or Console
# For Windows: use 'cls'
# For Linux and Mac, keep it as it is
os.system('clear')

# Getting the network i/o stats again to
# count the sending and receiving speed
netStats2 = psutil.net_io_counters()

# Upload/Sending speed
uploadStat = netStats2.bytes_sent - dataSent
# Receiving/Download Speed
downloadStat = netStats2.bytes_recv - dataRecv

# Print the Data
printData()

# Agian getting the data of total bytes sent and received
dataSent = netStats2.bytes_sent
dataRecv = netStats2.bytes_recv

Output


Summary

In this tutorial, we build a Python Program for Network Traffic Monitoring. We used the psutil library to get the network information and prettytable library to print the data in a nice tabular form.

Run this program side by side with your task manager (or System Monitor) open, to see if the program is giving proper output. If you have any doubts, please leave your comment below. You will get a reply soon.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post