0、安装与导入psutil
pip install psutil
import psutil
1、查看CPU信息
>>> psutil.cpu_count() #查看CPU核数
2
>>> psutil.cpu_count(logical=False) #查看CPU物理个数
2
>>> psutil.cpu_percent() #查看CPU使用率
23.4
>>> psutil.cpu_percent(percpu=True) #查看每个CPU的使用率
[18.9, 26.0]
>>> psutil.cpu_times() #查看CPU时间分配情况
scputimes(user=2750.625, system=2876.796875, idle=19316.078125)
2、查看开机时间
>>> import datetime
>>> datetime.datetime.fromtimestamp(psutil.boot_time()) #查看开机时间
datetime.datetime(2015, 7, 28, 7, 17, 50, 125000)
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S')
'2015-07-28 07:17:49'
3、查看内存信息
>>> virtual_memory = psutil.virtual_memory()
>>> virtual_memory.total /1024/1024/1024
2L
>>> virtual_memory.used
1909178368L
>>> virtual_memory.used/1024/1024/1024
1L
>>> virtual_memory.free/1024/1024/1024
1L
>>> virtual_memory.percent
60.0
>>> psutil.swap_memory()
sswap(total=4093583360L, used=2750255104L, free=1343328256L, percent=67.2, sin=0, sout=0)
>>> map(lambda x :x/1024/1024/1024,_)
[3L, 2L, 1L, 6.258487701416016e-08, 0, 0]
4、查看磁盘信息
>>> psutil.disk_partitions() #查看所有分区信息
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='CDFS',
opts='ro,cdrom'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom'), sdiskpart(device='F:\\', mountpoint='F:\\',
fstype='NTFS', opts='rw,fixed'), sdiskpart(device='G:\\', mountpoint='G:\\', fstype='', opts='cdrom'), sdiskpart(device='H:\\',
mountpoint='H:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='I:\\', mountpoint='I:\\', fstype='NTFS', opts='rw,fixed')]
>>> psutil.disk_usage('c:\\') #查看指定分区的磁盘空间情况
sdiskusage(total=39925473280L, used=33699393536L, free=6226079744L, percent=84.4)
>>> psutil.disk_io_counters(perdisk=True) #查看硬盘读写操作情况
{'PhysicalDrive0': sdiskio(read_count=111439, write_count=257966, read_bytes=3963010048L, write_bytes=3945559040L, read_time=101026455L,
write_time=48928114L)}
5、查看网络流量与收发包信息
>>> psutil.net_io_counters()
snetio(bytes_sent=29524224, bytes_recv=81521587, packets_sent=117718, packets_recv=162906, errin=0, errout=0, dropin=0, dropout=0)
6、查看当前登录用户信息
>>> psutil.users()
[suser(name=u'Administrator', terminal=None, host='0.242.33.0', started=1438039269.0)]
7、查看进程信息
>>> psutil.pids() 查看当前所有进程id
[0, 4, 612, 664, 696, 740, 752, 1008, 1028, 1108, 1232, 1356, 1392, 1428, 1544, 1692, 1756, 1776, 1872, 1928, 412, 668, 2140, 2152, 2164,
2172, 2192, 2200, 2208, 2224, 2304, 2332, 2352, 2376, 2400, 2448, 2516, 2556, 2588, 2652, 2696, 2756, 2808, 2864, 3080, 3136, 3184, 3192,
3244, 3264, 3332, 3720, 3796, 3824, 3872, 3908, 3976, 4024, 2688, 2684, 964, 4152, 4568, 4660, 4980, 5412, 5644, 5804, 4396, 4880, 5072,
6032, 4576, 7036, 5020, 5192, 7548, 1352, 1264, 5816, 8596, 5508, 9124, 8420, 9512, 9416, 2904, 5444, 10696, 11308, 10608, 11564, 10912,
4204, 12056, 8640, 9248, 11180, 10348]
>>> p = psutil.Process(4204) #获取指定id的进程
>>> p.name() #进程名
u'QQBrowser.exe'
>>> p.username() #查看创建该进程的用户名
u'HP6531\\Administrator'
>>> p.cmdline() #查看该进程对应的exe文件
['C:\\Program Files\\Tencent\\QQBrowser\\QQBrowser.exe', '-host=tab', '-scope=8420', '-Cred=624', '-group=0', '-tid=10', '-core=1',
'/prefetch:2']
>>> p.cwd()
'C:\\Program Files\\Tencent\\QQBrowser'
>>> p.exe()
u'C:\\Program Files\\Tencent\\QQBrowser\\QQBrowser.exe'
>>> p.cpu_affinity() #该进程CPU占用情况(运行在哪个CPU上)
[0, 1]
>>> p.num_threads() #该进程包含的线程数量
14
>>> p.threads() #该进程所有线程对象
[pthread(id=11400, user_time=0.1875, system_time=0.1875), pthread(id=12164, user_time=0.0, system_time=0.0), pthread(id=10636,
user_time=0.0, system_time=0.0), pthread(id=8964, user_time=0.0, system_time=0.015625), pthread(id=10872, user_time=0.015625,
system_time=0.046875), pthread(id=12228, user_time=0.0, system_time=0.0), pthread(id=11528, user_time=0.0, system_time=0.0), pthread
(id=5900, user_time=0.0, system_time=0.0), pthread(id=10472, user_time=0.0, system_time=0.0), pthread(id=11100, user_time=0.0,
system_time=0.0), pthread(id=4116, user_time=0.0, system_time=0.015625), pthread(id=8544, user_time=0.0, system_time=0.015625), pthread
(id=8768, user_time=0.0, system_time=0.0), pthread(id=10616, user_time=0.640625, system_time=0.328125)]
>>> p.status() #进程状态
'running'
>>> p.is_running()
True
>>> p.suspend() #挂起
>>> p.status()
'stopped'
>>> p.is_running()
True
>>> p.resume() #恢复运行
>>> p.status()
'running'
>>> p.kill() #结束进程
8、作为一个小例子,下面的代码检查记事本程序是否在运行,如果是,则返回记事本程序对应的进程id
>>> for id in psutil.pids():
try:
p = psutil.Process(id)
if os.path.basename(p.exe()) == 'notepad.exe':
print id
except:
pass
11180