import psutil
# cpu信息
print(psutil.cpu_times())
print(psutil.cpu_times().user)
print(psutil.cpu_times(percpu=True))
print(psutil.cpu_count())
print(psutil.cpu_count(logical=False))
scputimes(user=14341.484375, system=31892.640625, idle=253024.328125, interrupt=612.9531240463257, dpc=484.04687666893005)
14341.484375
[scputimes(user=1237.671875, system=4036.1875, idle=19665.515625, interrupt=456.84375, dpc=432.859375), scputimes(user=853.515625, system=2671.65625, idle=21412.9375, interrupt=19.546875, dpc=12.6875), scputimes(user=1475.421875, system=3457.0, idle=20005.6875, interrupt=18.0625, dpc=5.515625), scputimes(user=1069.65625, system=1860.515625, idle=22007.9375, interrupt=10.046875, dpc=3.484375238418579), scputimes(user=1105.8125, system=2307.3125, idle=21524.984375, interrupt=15.40625, dpc=4.09375), scputimes(user=797.40625, system=1598.765625, idle=22541.921875, interrupt=9.015625, dpc=2.5625), scputimes(user=1216.234375, system=2701.328125, idle=21020.53125, interrupt=14.062499046325684, dpc=3.859375238418579), scputimes(user=1294.15625, system=2924.171875, idle=20719.765625, interrupt=11.765625, dpc=2.953125), scputimes(user=1331.984375, system=2868.234375, idle=20737.875, interrupt=16.15625, dpc=4.046875476837158), scputimes(user=1530.375, system=2701.765625, idle=20705.953125, interrupt=12.828125, dpc=4.078125), scputimes(user=1506.59375, system=3070.40625, idle=20361.078125, interrupt=20.171875, dpc=4.359375476837158), scputimes(user=922.65625, system=1695.28125, idle=22320.140625, interrupt=9.046875, dpc=3.546875238418579)]
12
6
# 内存信息
import psutil
print(psutil.virtual_memory())
print()
print(psutil.virtual_memory().total)
print(psutil.virtual_memory().free)
print(psutil.swap_memory())
svmem(total=17032671232, available=7689572352, percent=54.9, used=9343098880, free=7689572352)
17032671232
7689576448
sswap(total=17032671232, used=10900594688, free=6132076544, percent=64.0, sin=0, sout=0)
#磁盘信息
import psutil
print(psutil.disk_partitions())
print()
print(psutil.disk_usage('/'))
print(psutil.disk_io_counters())
print(psutil.disk_io_counters(perdisk=True))
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')]
sdiskusage(total=255330144256, used=86329270272, free=169000873984, percent=33.8)
sdiskio(read_count=383575, write_count=622978, read_bytes=20437633536, write_bytes=19317652992, read_time=413, write_time=766)
{'PhysicalDrive0': sdiskio(read_count=33128, write_count=97344, read_bytes=3099861504, write_bytes=3454506496, read_time=10, write_time=25), 'PhysicalDrive1': sdiskio(read_count=331829, write_count=524894, read_bytes=16714242048, write_bytes=15698278400, read_time=356, write_time=731), 'PhysicalDrive2': sdiskio(read_count=18618, write_count=740, read_bytes=623529984, write_bytes=164868096, read_time=47, write_time=10)}
#网络信息
import psutil
temp = psutil.net_io_counters()
print(temp)
print()
print(psutil.net_io_counters(pernic=True))
print()
snetio(bytes_sent=95272147, bytes_recv=2235950654, packets_sent=377306, packets_recv=1757511, errin=0, errout=3, 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), '以太网 2': 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=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=5882, bytes_recv=58, packets_sent=5881, packets_recv=58, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=14442, bytes_recv=58, packets_sent=14442, packets_recv=58, errin=0, errout=0, dropin=0, dropout=0), '以太网 4': 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=95192389, bytes_recv=2235945674, packets_sent=356540, packets_recv=1757363, 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), '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), '本地连接* 4': snetio(bytes_sent=59434, bytes_recv=4864, packets_sent=443, packets_recv=32, errin=0, errout=3, dropin=0, dropout=0), 'isatap.{026CCF7B-71A6-44CF-82EC-4FC567660B95}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{C4BC60F4-1E02-4C55-8614-1913EB35FE31}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'isatap.{C05F6ED6-2FC2-4E97-B9A6-6F8584FFC50F}': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
#其他系统信息
import psutil
import datetime
print(psutil.users())
print(psutil.boot_time())
print(datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
[suser(name='V5', terminal=None, host='0.4.0.0', started=1542510378.0, pid=None)]
1542510362.0
2018-11-18 11:06:02
#进程
import psutil
pid = psutil.pids()[-1]
p = psutil.Process(pid)
print(p.name(),p.exe())
print(p.cwd(),p.status(),p.create_time(),p.cpu_times(),p.cpu_affinity(),p.memory_percent(),p.memory_info(),p.io_counters(),p.connections(),p.num_threads())
conhost.exe C:\Windows\System32\conhost.exe
C:\WINDOWS running 1542536824.0 pcputimes(user=0.015625, system=0.03125, children_user=0.0, children_system=0.0) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 0.05790734680224233 pmem(rss=9863168, vms=5173248, num_page_faults=2492, peak_wset=9863168, wset=9863168, peak_paged_pool=164848, paged_pool=164624, peak_nonpaged_pool=7864, nonpaged_pool=7864, pagefile=5173248, peak_pagefile=5173248, private=5173248) pio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, other_count=402, other_bytes=870) [] 4
# popen类的使用
import psutil
from subprocess import PIPE
p = psutil.Popen(["python", "-c", "print('hello,popen')"], stdout=PIPE)
print(p.name())
print(p.username())
print(p.communicate())
python.exe
DESKTOP-U8OTTDB\V5
(b'hello,popen\r\n', None)