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}")
print("\n")
# Continuously monitor network traffic every second
while True:
monitor_network()
time.sleep(1)
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.