Installation is simple
The code is as follows:
Pip Install Psutil
The official website address is:
https://pythonhosted.org/psutil/(with detailed API on the document)
GitHub addresses are:
https://github.com/giampaolo/psutil/
Psutil better Place, one is cross-platform, do not need to switch the platform when the re-open, another benefit of the tool centralized CPU, memory, disks, network, this information can be obtained.
Can be used to do system monitoring, performance analysis, process management. Supported systems are Linux, Windows, OSX, FreeBSD and Sun solaris,32 and 64-bit systems supported, while supporting pyhton2.4 to 3.4.
To see how good cross-platform is, under Windows Labs
The code is as follows:
#-*-Coding:utf-8-*-
#python2.7x
#author: orangleliu@gmail.com 2014-12-12
#psutiltest. py
'''''
Follow the tutorial easy to learn the use of Psutil, windows to try
'''
Import Psutil
Import datetime
#查看cpu的信息
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 ""
#查看内存信息
Print U "System total memory%s M"% (psutil. total_phymem/1024/1024)
Print U "System available memory%s M"% (Psutil.avail_phymem ()/1024/1024)
mem_rate = Int (Psutil.avail_phymem ())/float (Psutil. TOTAL_PHYMEM)
Print U "system memory usage%s percent"%int (mem_rate*100)
#系统启动时间
Print U "System boot time%s"%datetime.datetime.fromtimestamp (Psutil.boot_time ()). Strftime ("%y-%m-%d%h:%m:%s")
#系统用户
Users_count = Len (Psutil.users ())
Users_list = ",". Join ([U.name for U in Psutil.users ()])
Print U "currently has%s users,%s"% (Users_count, users_list)
#网卡, you can get the network card properties, connection number, 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 receive traffic%s NIC send traffic%s"% (BYTES_RCVD, bytes_sent)
Various detailed parameters of the #进程 process
#磁盘 The amount of disk usage, etc.
From this simple case, it can be seen that Psuti is powerful, so it works well on window, and it is very suitable for system data collection.
Please refer to the official documentation for details if you need to use it.
There are also some code snippets on gist to refer to Https://gist.github.com/search?q=psutil