What is the psutil.net_io_counters() method?
The psutil module
The psutil (Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). We mainly use it for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via pip as follows:
pip install psutil
The net_io_counters method
The net_io_counters method returns the system wide network input/output statistics in the form of counters as a tuple. The different counters returned are as follows:
| Field name | Description |
|---|---|
bytes_sent |
The number of bytes sent. |
bytes_recv |
The number of bytes received. |
packets_sent |
The number of packets sent. |
packets_recv |
The number of packets received. |
errin |
The total number of errors while receiving the packets. |
errout |
The total number of errors while sending the packets. |
dropin |
The total number of incoming packets that were dropped. |
dropout |
The total number of outgoing packets that were dropped. |
Method signature
psutil.net_io_counters(pernic=False, nowrap=True)
pernic: IfTrue, the method returns the same information for all the different network interface cards on the system.nowrap: On some systems, such as Linux, the numbers reported by the kernel may overflow and wrap on an extremely busy or long-lived system. SettingnowraptoTrueadjusts the values such that it increases or remains the same but never decreases.
Example
import psutilprint("psutil.net_io_counters() = ", psutil.net_io_counters())
Explanation
- Line 1: We import the
psutilmodule. - Line 3: We retrieve the current network IO statistics and print them to the console.