Posted on Leave a comment

Python Script to Monitor Network Traffic

Here is a simple Python script to monitor network traffic.

import psutil

def monitor_network():
    # Get the network I/O statistics for all interfaces
    net_io_counters = psutil.net_io_counters(pernic=True)

    # Loop through each interface
    for interface, info in net_io_counters.items():
        print(f"Interface: {interface}")
        print(f"Bytes sent: {info.bytes_sent}")
        print(f"Bytes received: {info.bytes_recv}")

# Continuously monitor network traffic every second
while True:

This script uses the psutil library to get the network I/O statistics for all network interfaces. It then loops through each interface and prints the number of bytes sent and received. The script continuously monitors the network traffic every second by using an infinite loop and the time.sleep function.

Leave a Reply

Your email address will not be published. Required fields are marked *