This article mainly introduces the Python psutil module Simple Use example, this article directly gives the use script, realizes the view CPU information, the view memory information, the view system start time, the view card information and so on, needs the friend to be possible to refer to under
Installation is simple
The code is as follows:
Pip Install Psutil
The official website address is:
https://pythonhosted.org/psutil/(with a detailed API on the document)
GitHub address is:
https://github.com/giampaolo/psutil/
Psutil is a better place, one is cross-platform, do not need to switch platform when reopened, another benefit of the Toolset CPU, memory, disks, network, this information can be obtained.
Can be used to do system monitoring, performance analysis, process management. The systems that can be supported are Linux, Windows, OS X, FreeBSD and Sun solaris,32 and 64-bit systems, supported by pyhton2.4 to 3.4.
In order to see the cross-platform good and bad, in Windows experiment
The code is as follows:
#-*-Coding:utf-8-*-
#python2.7x
#author: orangleliu@gmail.com 2014-12-12
#psutiltest. py
'''''
Follow the tutorial to learn the use of Psutil, windows to try
'''
Import Psutil
Import datetime
#查看cpu的信息
Print U "CPU number%s"%psutil.cpu_count ()
Print U "number of physical CPUs%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 free 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)
#系统启动时间
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, the number of 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 "network card receive traffic%s NIC send traffic%s"% (BYTES_RCVD, bytes_sent)
#进程 the various detailed parameters of a process
#磁盘 The amount of disk usage, and so on
From this simple case can be seen Psuti strong, in the window also so good, do system data acquisition is very appropriate.
Please refer to the official documentation in detail if you need to use it.
There are also some code snippets on the gist that can refer to Https://gist.github.com/search?q=psutil