Psutil profile

Psutil is a cross-platform library in Python to retrieve information running processes and system utilization (CPU, memory, disk, network, sensor) tools for system monitoring, analysis, and limiting process resources and management running.

If you have Anaconda installed, psutil can be used. If you have PIP installed, psutil can be used.

import psutilCopy the code

Brief introduction of main methods

Psutil.disk_partitions () returns information about a disk partition, including (device, mountpoint, fSType, opTS);

Psutil.disk_usage () Returns the disk usage:

disk = psutil.disk_partitions()
for i in disk:
    print("Disk :%s Partition format :%s" % (i.device, i.fstype)) Drive letter partition format
    disk_use = psutil.disk_usage(i.device) 

    print("Used: %.1f GB, free: %.1f GB, total: %.1f GB, Usage %. % (
        disk_use.used / 1024 / 1024 / 1024, disk_use.free / 1024 / 1024 / 1024, disk_use.total / 1024 / 1024 / 1024,
        disk_use.percent))Copy the code
Disk: C:\ Partition format :NTFS uses:34.8GB, spare time:48.2GB, a total of:83.0GB and utilization41.9%, disk: D:\ Partition format :NTFS uses:110.5GB, spare time:89.2GB, a total of:199.7GB and utilization55.4%, disk: E:\ Partition format :NTFS uses:100.1GB, spare time:95.2GB, a total of:195.3GB and utilization51.3%, disk: F:\ Partition format :NTFS uses:120.6GB, spare time:64.4GB, a total of:184.9GB and utilization65.2%,  Copy the code

Psutil.cpu_percent () Indicates the CPU usage

Psutil.virtual_memory () Memory status

memory = psutil.virtual_memory()
# memory.used
# memory. Total altogether
ab = float(memory.used) / float(memory.total) * 100
print("Memory usage :%.2f%%" % ab)Copy the code

Psutil.net _io_counters() Network usage, which can monitor the upload and download information of each network port on the computer; Each computer returns a different message because of the name of the network port. Print out the network port information of your computer with the following code:

print(psutil.net_io_counters(pernic=True))Copy the code

You’ll get something like the following:

{'Ethernet': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'Local connection * 2': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet1': snetio(bytes_sent=597, bytes_recv=13, packets_sent=596, packets_recv=13, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=1919, bytes_recv=13, packets_sent=1919, packets_recv=13, errin=0, errout=0, dropin=0, dropout=0), 'WLAN': snetio(bytes_sent=3993804, bytes_recv=76316885, packets_sent=35011, packets_recv=63467, errin=0, errout=0, dropin=0, dropout=0), 'Bluetooth Network Connection': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'Loopback Pseudo-Interface 1': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'Teredo Tunneling Pseudo-Interface': snetio(bytes_sent=13724, bytes_recv=760, packets_sent=102, packets_recv=5, errin=0, errout=0, dropin=0, dropout=0)}Copy the code

Now you can happily monitor broadband or WLAN upload and download speeds with the following code:

import psutil
import time

def net_state(a):
   
    recv1 = psutil.net_io_counters(pernic=True) ['WLAN'] [1] # Receive data
    send1 = psutil.net_io_counters(pernic=True) ['WLAN'] [0] # upload data
    time.sleep(1)  Listen port to receive data every 1s
    recv2 = psutil.net_io_counters(pernic=True) ['WLAN'] [1]
    send2 = psutil.net_io_counters(pernic=True) ['WLAN'] [0]
    # upload data
    return 'upload:%.1f kb/s.' % ((send2 - send1) / 1024.0), 'download:%.1f kb/s.' % ((recv2 - recv1) / 1024.0)

while True:
    s1 = net_state()[0]
    s2 = net_state()[1]
    print('The current upload and download speed is :')
    print(s1)
    print(s2)
    print('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -')Copy the code

If you don’t want to run this script every time, you can use PyInstaller to package it. Then run it as an exe executable.