Python psutil module simple use example
This article mainly introduces the Python psutil module's simple examples. This article provides a script to view cpu information, memory information, system startup time, and network card information, for more information, see
Easy to install
The Code is as follows:
Pip install psutil
Official website address:
Https://pythonhosted.org/psutil/ (detailed api on Documentation)
Github address:
Https://github.com/giampaolo/psutil/
Psutil is better. One is cross-platform, and it is re-opened when you do not need to switch the platform. Another good tool is CPU, memory, disks, network, this information can be obtained.
It can be used for system monitoring, performance analysis, and process management. Supported systems include Linux, Windows, OSX, FreeBSD and Sun Solaris, 32, and 64-bit systems, and pyhton2.4 to 3.4.
In order to see whether cross-platform services are useful
The Code is as follows:
#-*-Coding: UTF-8 -*-
# Python2.7x
# Author: orangleliu@gmail.com
# Psutiltest. py
'''''
Follow the tutorial to learn how to use psutil. Try it in windows.
'''
Import psutil
Import datetime
# View cpu Information
Print u "CPU count % s" % psutil. cpu_count ()
Print u "physical CPU count % s" % psutil. cpu_count (logical = False)
Print u "CPU uptimes"
Print psutil. cpu_times ()
Print ""
# View memory information
Print u "total system memory % s M" % (psutil. TOTAL_PHYMEM/1024/1024)
Print u "available system memory % s M" % (psutil. avail_phymem ()/1024/1024)
Mem_rate = int (psutil. avail_phymem ()/float (psutil. TOTAL_PHYMEM)
Print u "system memory usage % s %" % int (mem_rate * 100)
# System startup time
Print u "system start time % s" % datetime. datetime. fromtimestamp (psutil. boot_time ()). strftime ("% Y-% m-% d % H: % M: % S ")
# System users
Users_count = len (psutil. users ())
Users_list = ",". join ([u. name for u in psutil. users ()])
Print u "currently has % s users, respectively % s" % (users_count, users_list)
# Nic. You can obtain Nic attributes, connections, current traffic, and other information.
Net = psutil.net _ io_counters ()
Bytes_sent = '{0:. 2f} kb'. format (net. bytes_recv/1024)
Bytes_rcvd = '{0:. 2f} kb'. format (net. bytes_sent/1024)
Print u "Nic received traffic % s Nic sent traffic % s" % (bytes_rcvd, bytes_sent)
# Detailed parameters of a process
# Disk usage and so on
In this simple case, we can see that the power of PstI is so powerful in Windows that it is very suitable for system data collection.
For more information, see the official documentation.
There are some code snippets on gist can refer to the https://gist.github.com/search? Q = psutil