psutil模块详解

 

import psutil
#1、系统性能信息模块psutil
mem = psutil.virtual_memory()
print(mem)
#svmem(total=8442675200, available=1900957696, percent=77.5, used=6541717504, free=1900957696)
#比如百分比显示的信息和我的360上使用内存百分比一致的,单位是字节 B
print(mem.total,mem.percent)
#8442675200 77.6
#1.1获取系统性能信息
#显示所有逻辑cpu信息
print(psutil.cpu_times(percpu=True))
#[scputimes(user=11973.46875, system=53070.921875, idle=71753.046875, interrupt=614.84375, dpc=556.765625), scputimes(user=7833.890625, system=14614.062499999985, idle=114349.15625, interrupt=200.015625, dpc=20.765625), scputimes(user=13077.875, system=64398.703125, idle=59320.53125, interrupt=261.5, dpc=16.203125), scputimes(user=10840.437499999998, system=21394.171875, idle=104562.49999999999, interrupt=193.5625, dpc=13.859375)]
#获取cpu信息,好像是总体的
print(psutil.cpu_times())
#scputimes(user=43860.14062499999, system=153661.00000000006, idle=350629.49999999994, interrupt=1274.59375, dpc=611.21875)
#获取单项数据信息,如用户user的cpu时间比
print(psutil.cpu_times().user)
#43989.67187499999
#获取cpu逻辑个数,默认logical=True4
print(psutil.cpu_count())
#4
#获取cpu物理个数
print(psutil.cpu_count(logical=True))

#===
#获取内存完整信息
mem=psutil.virtual_memory()
print(mem)
#svmem(total=8442675200, available=2253422592, percent=73.3, used=6189252608, free=2253422592)
#获取内存总数
print(mem.total)
#8442675200
#获取空闲内存数
print(mem.free)
#2252984320
#获取swap分区信息
print(psutil.swap_memory())
#sswap(total=18643222528, used=13967691776, free=4675530752, percent=74.9, sin=0, sout=0)

#======
#获取磁盘完整信息
print(psutil.disk_partitions())
#[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed', maxfile=255, maxpath=260)]
print(type(psutil.disk_partitions()))#<class 'list'>
#获取分区的使用情况(单个磁盘):
print(psutil.disk_usage('c:\\'))
#sdiskusage(total=129176965120, used=97699663872, free=31477301248, percent=75.6)
#获取磁盘总的io个数,读写信息
print(psutil.disk_io_counters())
#sdiskio(read_count=909190, write_count=1472997, read_bytes=63249928704, write_bytes=40258563072, read_time=21921, write_time=8639)
#获取单个分区的io个数,读写信息
print(psutil.disk_io_counters(perdisk=True))
#{'PhysicalDrive0': sdiskio(read_count=909444, write_count=1474143, read_bytes=63257588224, write_bytes=40283741184, read_time=21923, write_time=8640)}

#======
#获取网络总的io信息,默认perinic=False
print(psutil.net_io_counters())
#snetio(bytes_sent=1840042559, bytes_recv=1945703253, packets_sent=3872624, packets_recv=5073757, errin=70, errout=0, dropin=70, dropout=0)
#输出每个网络接口的IO信息
print(psutil.net_io_counters(pernic=True))
#字典{'以太网 3': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '以太网 5': snetio(bytes_sent=640579926, bytes_recv=184115712, packets_sent=958984, packets_recv=600803, errin=0, errout=0, dropin=0, dropout=0), '本地连接* 11': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '本地连接* 12': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '以太网 2': snetio(bytes_sent=1200771752, bytes_recv=1603325755, packets_sent=2831223, packets_recv=4373762, errin=70, errout=0, dropin=70, dropout=0), 'VMware Network Adapter VMnet1': snetio(bytes_sent=39751, bytes_recv=310, packets_sent=39751, packets_recv=310, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=39397, bytes_recv=0, packets_sent=39398, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), '蓝牙网络连接': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'WLAN': snetio(bytes_sent=1289787, bytes_recv=159911181, packets_sent=15921, packets_recv=112562, 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)}
#=======
#返回当前登录系统的用户信息
print(type(psutil.users()),psutil.users())
#<class 'list'> [suser(name='Administrator', terminal=None, host=None, started=1636939012.6702487, pid=None)]
#获取开机时间,时间戳格式返回
print(psutil.boot_time())
#1636938961.3155193
#转换成自然格式的开机时间
import datetime
print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
#2021-11-15 09:16:01

#=====
#列出所有进程PID
print(psutil.pids())
#[0, 4, 72, 100, 368, 488, 520, 688, 696, 804, 808, 820, 880, 888, 1012, 1028, 1108, 1148, 1160, 1236, 1292, 1300, 1364, 1372, 1400, 1468, 1480, 1508, 1548, 1568, 1592, 1604, 1632, 1644, 1660, 1668, 1676, 1684, 1764, 1924, 1932, 1956, 2008, 2052, 2076, 2136, 2148, 2164, 2208, 2212, 2252, 2268, 2308, 2324, 2376, 2392, 2412, 2440, 2488, 2496, 2520, 2528, 2540, 2560, 2580, 2628, 2684, 2696, 2756, 2816, 2928, 3024, 3096, 3140, 3196, 3252, 3268, 3308, 3328, 3372, 3380, 3452, 3508, 3616, 3636, 3716, 3852, 3880, 3904, 3920, 4116, 4160, 4168, 4208, 4220, 4276, 4344, 4524, 4548, 4584, 4596, 4624, 4700, 4824, 4860, 4908, 4928, 4936, 4944, 4952, 4960, 4968, 4976, 4984, 4992, 5000, 5008, 5036, 5092, 5104, 5108, 5156, 5164, 5172, 5196, 5216, 5220, 5244, 5288, 5296, 5316, 5356, 5432, 5440, 5452, 5476, 5544, 5652, 5688, 5728, 5848, 5876, 5884, 5920, 6336, 6472, 6520, 6556, 6684, 6820, 6920, 7016, 7216, 7264, 7268, 7312, 7496, 7528, 7572, 7604, 7608, 7700, 7780, 7816, 7876, 7936, 7952, 8060, 8168, 8180, 8280, 8428, 8464, 8664, 8752, 8784, 8812, 8816, 8872, 8972, 9032, 9048, 9076, 9088, 9224, 9300, 9340, 9364, 9448, 9600, 9688, 9740, 9844, 9904, 9960, 10176, 10244, 10328, 10384, 10388, 10432, 10480, 10556, 10564, 10720, 10728, 10876, 11208, 11328, 11332, 11740, 11780, 11832, 11968, 12080, 12144, 12180, 12224, 12728, 12872, 13056, 13304, 13320, 13448, 13500, 13540, 13872, 13992, 14180, 14272, 14320, 14420, 14484, 14616, 14856, 14968, 15024, 15220, 15328, 15584, 15640, 16284, 16500, 16672, 17232, 17568, 17756, 17828, 18268, 18328, 18340, 18376, 18696, 18844, 18860, 19012, 19016, 19060, 19256, 19300, 19352, 19432, 19524, 19688, 19728, 19760, 19860, 19956, 20076, 20144, 20196, 20240, 20256, 20316, 20716, 20728, 20784, 21064, 21200]
#实例化一个Process对象,参数为一进程PID;即获取指定pid的进程名
p=psutil.Process(520)
print(p.name())
#dllhost.exe
#获取指定pid的进程bin路径
print(p.exe())
#C:\Windows\System32\dllhost.exe
#指定进程工作目录绝对路径
print(p.cwd())
#C:\Windows\system32
#指定进程状态
print(p.status())
#running
#指定进程的创建时间,时间戳格式
print(p.create_time())
#1636939051.8588655
#指定进程uid信息
# print(p.uids())
#win好像没有uid AttributeError: 'Process' object has no attribute 'uids'
#指定进程的gid信息
# print(p.gids())
#win应该没有gid
#进程cpu时间信息,包括user、system两个cpu时间
print(p.cpu_times)
#<bound method Process.cpu_times of psutil.Process(pid=520, name='dllhost.exe', status='running', started='2021-11-15 09:17:31')>
#貌似没有cpu用户和系统时间,可能是操作系统是win的原因吧
#获取cpu亲和度,如要设置进程cpu亲和度,将cpu号作为参数即可
print(p.cpu_affinity())
#[0, 1, 2, 3]
#进程内存利用率
print(p.memory_percent())
#0.03454298466912478
#进程内存rss,vms信息
print(p.memory_info)
#win的估计不支持,又不是内存信息的<bound method Process.memory_info of psutil.Process(pid=520, name='dllhost.exe', status='running', started='2021-11-15 09:17:31')>
#进程IO信息,包括读写IO数及字节数
print(p.io_counters())
#pio(read_count=37, write_count=137, read_bytes=3260416, write_bytes=2072576, other_count=2032, other_bytes=45620)
#返回打开进程socket的namedutples列表,包括fs,family,laddr等信息
print(p.connections())
#[]
#进程开启的线程数
print(p.num_threads())
#4

#====popen类的使用
from subprocess import PIPE
# p=psutil.Popen(["/usr/bin/python","-c","print('mcw')"],stdout=PIPE)
p.name()
#'python'
print(p.username())
#'root'
print(p.communicate())
#('hello\n',None)
print(p.cpu_times)
#pcputimes(user=0.001,system=xxx)
#参考地址 https://gihub.com/giampaolo/psutil http://psutil.readthedocs.org/en/latest

参考书籍:python自动化运维技术与最佳实践 刘天斯





上一篇:Python之TCP网络编程(Socket)


下一篇:python发送文件