0% found this document useful (0 votes)
4 views

computer network project

Uploaded by

chahmedk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

computer network project

Uploaded by

chahmedk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Documentation for Network Traffic Analyzer Code

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:

 psutil: Provides system and network statistics.

 prettytable: Used for formatting and displaying the data in a table.

Install the libraries using pip:

pip install psutil prettytable

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.

 Shows the real-time upload (sent) and download (received) speeds.

 Continuously updates the table at regular intervals (10 seconds).

 Clears the console between updates to maintain a clean output.

4. Code Explanation

4.1 Imports

import os

import time
import psutil

from prettytable import PrettyTable

from prettytable import DOUBLE_BORDER

 os: Provides a portable way of using operating system-dependent functionality like clearing the
console screen.

 time: Used to introduce a delay between updates.

 psutil: Used to fetch network statistics (e.g., bytes sent and received).

 prettytable: Provides functionality for generating formatted tables, used to display network
data.

4.2 Units of Memory Sizes

size = ['bytes', 'KB', 'MB', 'GB', 'TB']

 This list defines the different units of memory sizes. The program uses these units to convert the
byte count into a human-readable format.

4.3 Function: getSize()

def getSize(bytes):

for unit in size:

if bytes < 1024:

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.

4.4 Function: printData()

def printData():

card = PrettyTable()

card.set_style(DOUBLE_BORDER)

card.field_names = ["Received", "Receiving", "Sent", 'Sending']


card.add_row([f"{getSize(netStats2.bytes_recv)}",

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:

o card.set_style(DOUBLE_BORDER): Applies double borders to the table for a cleaner


appearance.

o card.field_names: Defines the column names of the table (e.g., "Received",


"Receiving").

o card.add_row(): Adds the current network statistics to the table.

o print(card): Prints the table to the console.

4.5 Network Statistics Retrieval and Initial Data Setup

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.

4.6 Main Loop

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:

1. The script pauses for 10 seconds using time.sleep(10).

2. It clears the console screen using os.system('clear') (works on Linux/macOS; use cls on
Windows).

3. It fetches the new network statistics (netStats2 = psutil.net_io_counters()).

4. The upload and download speeds are calculated as the difference in bytes sent and
received since the last check.

5. The printData() function is called to display the updated statistics.

6. The script updates the dataSent and dataRecv variables with the new values to use
them in the next cycle.

5. Output Example

The output will be displayed as a table, and it updates every 10 seconds:

Columns:

 Received: Total data received (in bytes, KB, MB, etc.).

 Receiving: Real-time download speed (in bytes, KB, MB per second).

 Sent: Total data sent (in bytes, KB, MB, etc.).

 Sending: Real-time upload speed (in bytes, KB, MB per second).

6. Example Usage
1. Install Required Libraries:

2. pip install psutil prettytable

3. Run the Script:

o Save the code in a file named network_traffic_analyzer.py.

o Open the terminal/command prompt and navigate to the directory where the script is
located.

o Run the script:

o python network_traffic_analyzer.py

The script will continuously display the network data in a table format every 10 seconds.

7. Limitations and Considerations

 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.

You might also like