computer network project
computer network project
This documentation describes the Network Traffic Analyzer Python script that monitors network traffic
and displays the statistics in a well-formatted table.
1. Overview
The Network Traffic Analyzer is a Python script that monitors network activity, including data sent and
received, and real-time upload and download speeds. The tool continuously updates and displays this
data every 10 seconds in a table format using the psutil and prettytable libraries.
2. Requirements
Before running the script, ensure the following libraries are installed:
3. Functionality
This script monitors the network statistics of the system and provides the following functionalities:
Displays the total amount of data received and sent over the network.
4. Code Explanation
4.1 Imports
import os
import time
import psutil
os: Provides a portable way of using operating system-dependent functionality like clearing the
console screen.
psutil: Used to fetch network statistics (e.g., bytes sent and received).
prettytable: Provides functionality for generating formatted tables, used to display network
data.
This list defines the different units of memory sizes. The program uses these units to convert the
byte count into a human-readable format.
def getSize(bytes):
return f"{bytes:.1f}{unit}"
bytes /= 1024
Purpose: Converts the byte count into a more readable format (e.g., KB, MB, GB).
Logic: The function iterates over the units of memory, dividing the number of bytes by 1024
until it finds the appropriate unit.
def printData():
card = PrettyTable()
card.set_style(DOUBLE_BORDER)
f"{getSize(downloadStat)}/s",
f"{getSize(netStats2.bytes_sent)}",
f"{getSize(uploadStat)}/s"])
print(card)
Purpose: This function formats and prints the network data in a table format using PrettyTable.
Key Components:
netStats1 = psutil.net_io_counters()
dataSent = netStats1.bytes_sent
dataRecv = netStats1.bytes_recv
The script uses psutil.net_io_counters() to retrieve the initial network statistics, including the
total number of bytes sent and received. These values are stored for later comparison to
calculate the upload and download speeds.
while True:
time.sleep(10)
os.system('clear')
netStats2 = psutil.net_io_counters()
uploadStat = netStats2.bytes_sent - dataSent
downloadStat = netStats2.bytes_recv - dataRecv
printData()
dataSent = netStats2.bytes_sent
dataRecv = netStats2.bytes_recv
Purpose: The main loop continuously retrieves network statistics every 10 seconds and updates the
displayed data.
Process:
2. It clears the console screen using os.system('clear') (works on Linux/macOS; use cls on
Windows).
4. The upload and download speeds are calculated as the difference in bytes sent and
received since the last check.
6. The script updates the dataSent and dataRecv variables with the new values to use
them in the next cycle.
5. Output Example
Columns:
6. Example Usage
1. Install Required Libraries:
o Open the terminal/command prompt and navigate to the directory where the script is
located.
o python network_traffic_analyzer.py
The script will continuously display the network data in a table format every 10 seconds.
Platform Compatibility:
o The script uses os.system('clear'), which works on Linux/macOS systems. For Windows,
use os.system('cls') instead.
Network Interface:
o The script shows data for the default network interface. If your system has multiple
interfaces, you may need to modify the script to handle specific interfaces.
Permissions:
o The script may require administrative or root privileges to access network statistics on
some systems.
Continuous Output:
o The script runs indefinitely until manually stopped. Use Ctrl + C to stop the script.
8. Conclusion
This Network Traffic Analyzer provides a simple way to monitor real-time network activity. By displaying
both the total data received/sent and the real-time speeds, the script gives a comprehensive overview
of the system's network usage. The table format makes the data easy to understand and track over
time.