牛骨文教育服务平台(让学习变的简单)
博文笔记

Python psutil库介绍

创建时间:2017-08-04 投稿人: 浏览次数:780

1.1pip安装

pip install  psutil

import psutil

1.2源码安装

首先从网上下载psutil的源码

tar zxf psutil-2.0.0.tar.gz

cd psutil-2.0.0

python setup.py install

2.1 CPU

查看CPU逻辑个数

>>> psutil.cpu_count()
4

查看CPU物理个数

>>> psutil.cpu_count(logical=False)
2

查看CPU使用率

>>> psutil.cpu_percent()
16.3

>>> for x in range(3):
...     psutil.cpu_percent(interval=1)
...
24.9
6.5
5.7

查看每个CPU的使用情况

>>> psutil.cpu_percent(percpu=True)
[8.9, 20.4, 11.1, 0.0]

查看CPU时间分配情况

>>> psutil.cpu_times()
scputimes(user=1078.497314453125, system=890.8125, idle=8801.919921875, interrup
t=72.61846363544464, dpc=24.80415964126587)

>>> psutil.cpu_times().user
1079.105712890625

>>> for x in range(3):
...     psutil.cpu_times_percent(interval=1,percpu=False)
...
scputimes(user=3.9, system=1.9, idle=94.2, interrupt=0.0, dpc=0.0)
scputimes(user=8.2, system=16.4, idle=75.4, interrupt=0.0, dpc=0.0)
scputimes(user=2.3, system=1.9, idle=95.3, interrupt=0.0, dpc=0.4)

查看CPU的工作频率

>>> psutil.cpu_freq()
scpufreq(current=1496.0, min=0.0, max=1700.0)

2.2内存

>>> psutil.swap_memory()
sswap(total=8387072000L, used=3215523840L, free=5171548160L, percent=38.3, sin=0
, sout=0)
>>> psutil.virtual_memory()
svmem(total=4194488320L, available=1636712448L, percent=61.0, used=2557775872L,
free=1636712448L)

>>> psutil.virtual_memory().total
4194488320L

2.3磁盘

查看所有分区信息

>>> psutil.disk_partitions()
[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"),

sdiskpart(device="F:\", mountpoint="F:\", fstype="NTFS", opts="rw,fixed"),

sdiskpart(device="G:\", mountpoint="G:\", fstype="", opts="cdrom")]
>>>

查看C盘使用情况

>>> psutil.disk_usage("c:\")
sdiskusage(total=53694595072L, used=50804322304L, free=2890272768L, percent=94.6
)

查看磁盘读写操作情况

>>> psutil.disk_io_counters()
sdiskio(read_count=173699, write_count=45921, read_bytes=4289825280L, write_byte
s=2067431424L, read_time=261191320L, write_time=37013250L)

>>> psutil.disk_io_counters(perdisk=True)
{"PhysicalDrive0": sdiskio(read_count=173699, write_count=45940, read_bytes=4289
825280L, write_bytes=2067515904L, read_time=261191320L, write_time=37014420L)}
>>>

2.4网络

>>> psutil.net_io_counters()
snetio(bytes_sent=7332613L, bytes_recv=60046192L, packets_sent=52025L, packets_recv=80645L, errin=0L, errout=0L, dropin=0L, dropout=0L)
>>> psutil.net_io_counters(pernic=True)
{"Teredo Tunneling Pseudo-Interface": snetio(bytes_sent=22660L, bytes_recv=27600L, packets_sent=304L, packets_recv=264L, errin=0L, errout=0L, dropin=0L, dropout
=0L),

"isatap.{4055306E-39BD-4FD5-9BF8-72E03E28110C}": snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L),

"本地连接": snetio(bytes_sent=7313461L, bytes
_recv=60023778L, packets_sent=51734L, packets_recv=80398L, errin=0L, errout=0L,dropin=0L, dropout=0L),

"Loopback Pseudo-Interface 1": snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L)}
>>>

>>> >>> psutil.net_io_counters(pernic=True)
{"Teredo Tunneling Pseudo-Interface": snetio(bytes_sent=24892L, bytes_recv=30008L, packets_sent=334L, packets_recv=286L, errin=0L, errout=0L, dropin=0L, dropout=0L), "isatap.{4055306E-39BD-4FD5-9BF8-72E03E28110C}": snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L), "xb1xbexb5xd8xc1xacxbdxd3": snetio(bytes_sent=8040994L, bytes_recv=64343651L, packets_sent=56244L, packets_recv=87058L, errin=0L, errout=0L, dropin=0L, dropout=0L), "Loopback Pseudo-Interface 1": snetio(bytes_sent=0L, bytes_recv=0L, packets_sent=0L, packets_recv=0L, errin=0L, errout=0L, dropin=0L, dropout=0L)}
>>>

>>> psutil.net_connections()
[sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 49168), raddr=("127.0.0.1", 49169), status="ESTABLISHED", pid=1680), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 54880), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("192.168.20.45", 63668), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=1, laddr=("192.168.1.45", 139), raddr=(), status="LISTEN", pid=4), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 50316), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 445), raddr=(), status="LISTEN", pid=4), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 50278), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=23, type=1, laddr=("::", 49153), raddr=(), status="LISTEN", pid=956), sconn(fd=-1, family=2, type=2, laddr=("192.168.3.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=23, type=1, laddr=("::", 49154), raddr=(), status="LISTEN", pid=392), sconn(fd=-1, family=23, type=1, laddr=("::", 49152), raddr=(), status="LISTEN", pid=564), sconn(fd=-1, family=23, type=2, laddr=("::1", 1900), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 27000), raddr=("127.0.0.1", 49170), status="ESTABLISHED", pid=1064), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 4301), raddr=(), status="LISTEN", pid=6164), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 50318), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 49200), raddr=("127.0.0.1", 49199), status="ESTABLISHED", pid=1068), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52197), raddr=("183.36.108.223", 443), status="TIME_WAIT", pid=0), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 3544), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49174), raddr=(), status="LISTEN", pid=624), sconn(fd=-1, family=23, type=1, laddr=("fe80::7929:9a86:77f0:1157", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52193), raddr=("180.97.10.109", 80), status="CLOSE_WAIT", pid=6164), sconn(fd=-1, family=23, type=1, laddr=("::", 135), raddr=(), status="LISTEN", pid=856), sconn(fd=-1, family=2, type=2, laddr=("192.168.4.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=1, laddr=("192.168.2.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 9410), raddr=(), status="LISTEN", pid=2444), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 49169), raddr=("127.0.0.1", 49168), status="ESTABLISHED", pid=1680), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 2103), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=23, type=2, laddr=("::", 4500), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 1433), raddr=(), status="LISTEN", pid=2268), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 27000), raddr=(), status="LISTEN", pid=1064), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 3389), raddr=(), status="LISTEN", pid=1188), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 49373), raddr=("220.181.132.166", 80), status="ESTABLISHED", pid=3672), sconn(fd=-1, family=23, type=2, laddr=("fe80::7929:9a86:77f0:1157", 1900), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 3600), raddr=(), status="NONE", pid=3672), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 5939), raddr=(), status="LISTEN", pid=2828), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49166), raddr=(), status="LISTEN", pid=1680), sconn(fd=-1, family=2, type=2, laddr=("192.168.100.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52190), raddr=("180.153.105.160", 80), status="CLOSE_WAIT", pid=6164), sconn(fd=-1, family=23, type=1, laddr=("::", 445), raddr=(), status="LISTEN", pid=4), sconn(fd=-1, family=2, type=1, laddr=("192.168.7.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=23, type=1, laddr=("::", 49155), raddr=(), status="LISTEN", pid=648), sconn(fd=-1, family=2, type=2, laddr=("192.168.5.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("192.168.1.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 10115), raddr=(), status="NONE", pid=2004), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 62552), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52156), raddr=("101.201.170.241", 80), status="ESTABLISHED", pid=1068), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 4501), raddr=(), status="LISTEN", pid=1784), sconn(fd=-1, family=2, type=2, laddr=("192.168.6.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("192.168.6.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("192.168.7.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 58424), raddr=(), status="NONE", pid=3672), sconn(fd=-1, family=2, type=1, laddr=("192.168.100.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 500), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=2, laddr=("192.168.3.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49175), raddr=(), status="LISTEN", pid=3368), sconn(fd=-1, family=23, type=2, laddr=("::1", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 5355), raddr=(), status="NONE", pid=1188), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 80), raddr=(), status="LISTEN", pid=4), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 2105), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("192.168.8.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=23, type=1, laddr=("::", 10115), raddr=(), status="LISTEN", pid=2004), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 9088), raddr=(), status="LISTEN", pid=5596), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 53454), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=23, type=1, laddr=("::", 49174), raddr=(), status="LISTEN", pid=624), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 137), raddr=(), status="NONE", pid=4), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49152), raddr=(), status="LISTEN", pid=564), sconn(fd=-1, family=23, type=1, laddr=("::", 2103), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("192.168.1.45", 52196), raddr=("192.168.1.84", 139), status="TIME_WAIT", pid=0), sconn(fd=-1, family=23, type=1, laddr=("::", 2107), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("192.168.5.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("192.168.4.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=23, type=1, laddr=("::", 49175), raddr=(), status="LISTEN", pid=3368), sconn(fd=-1, family=23, type=1, laddr=("::", 2105), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=23, type=2, laddr=("fe80::7929:9a86:77f0:1157", 62550), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 52187), raddr=("127.0.0.1", 52188), status="ESTABLISHED", pid=520), sconn(fd=-1, family=23, type=1, laddr=("::", 2383), raddr=(), status="LISTEN", pid=2316), sconn(fd=-1, family=2, type=2, laddr=("127.0.0.1", 1900), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=2, laddr=("192.168.7.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 49199), raddr=("127.0.0.1", 49200), status="ESTABLISHED", pid=1068), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52195), raddr=("47.95.49.160", 80), status="TIME_WAIT", pid=0), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52163), raddr=("101.201.173.115", 80), status="ESTABLISHED", pid=1068), sconn(fd=-1, family=2, type=2, laddr=("192.168.20.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 60299), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49155), raddr=(), status="LISTEN", pid=648), sconn(fd=-1, family=2, type=2, laddr=("192.168.6.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 10115), raddr=(), status="LISTEN", pid=2004), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 55440), raddr=(), status="NONE", pid=2444), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 1434), raddr=(), status="LISTEN", pid=2268), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49171), raddr=(), status="LISTEN", pid=1784), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 52194), raddr=("47.95.49.160", 80), status="TIME_WAIT", pid=0), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49173), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 138), raddr=(), status="NONE", pid=4), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 50317), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=2, type=2, laddr=("192.168.5.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("127.0.0.1", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49153), raddr=(), status="LISTEN", pid=956), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 4023), raddr=(), status="NONE", pid=6164), sconn(fd=-1, family=2, type=2, laddr=("192.168.8.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 49154), raddr=(), status="LISTEN", pid=392), sconn(fd=-1, family=2, type=1, laddr=("192.168.3.45", 1801), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("192.168.1.45", 1900), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=23, type=2, laddr=("::", 5355), raddr=(), status="NONE", pid=1188), sconn(fd=-1, family=23, type=1, laddr=("::", 49173), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 2383), raddr=(), status="LISTEN", pid=2316), sconn(fd=-1, family=23, type=1, laddr=("::", 1433), raddr=(), status="LISTEN", pid=2268), sconn(fd=-1, family=2, type=2, laddr=("192.168.20.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 4500), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=2, laddr=("192.168.2.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("192.168.8.45", 5353), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 49170), raddr=("127.0.0.1", 27000), status="ESTABLISHED", pid=1680), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 49179), raddr=("140.206.78.32", 80), status="ESTABLISHED", pid=3672), sconn(fd=-1, family=23, type=1, laddr=("::", 80), raddr=(), status="LISTEN", pid=4), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 2107), raddr=(), status="LISTEN", pid=2176), sconn(fd=-1, family=2, type=2, laddr=("192.168.2.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=23, type=2, laddr=("::", 10115), raddr=(), status="NONE", pid=2004), sconn(fd=-1, family=2, type=2, laddr=("192.168.100.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=23, type=1, laddr=("::1", 1434), raddr=(), status="LISTEN", pid=2268), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 52188), raddr=("127.0.0.1", 52187), status="ESTABLISHED", pid=6884), sconn(fd=-1, family=2, type=2, laddr=("192.168.4.45", 123), raddr=(), status="NONE", pid=2408), sconn(fd=-1, family=23, type=2, laddr=("::", 500), raddr=(), status="NONE", pid=392), sconn(fd=-1, family=2, type=2, laddr=("127.0.0.1", 62553), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 52187), raddr=(), status="LISTEN", pid=520), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 50741), raddr=("101.201.173.115", 80), status="ESTABLISHED", pid=1068), sconn(fd=-1, family=23, type=1, laddr=("::", 3389), raddr=(), status="LISTEN", pid=1188), sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 135), raddr=(), status="LISTEN", pid=856), sconn(fd=-1, family=23, type=2, laddr=("::", 54881), raddr=(), status="NONE", pid=2828), sconn(fd=-1, family=2, type=2, laddr=("0.0.0.0", 54882), raddr=(), status="NONE", pid=1616), sconn(fd=-1, family=2, type=1, laddr=("127.0.0.1", 4300), raddr=(), status="LISTEN", pid=6164), sconn(fd=-1, family=23, type=2, laddr=("::1", 62551), raddr=(), status="NONE", pid=1452), sconn(fd=-1, family=2, type=1, laddr=("192.168.20.45", 50762), raddr=("101.201.173.115", 80), status="ESTABLISHED", pid=1068)]
>>>
>>> >>> psutil.net_if_addrs()
{"Teredo Tunneling Pseudo-Interface": [snic(family=-1, address="00-00-00-00-00-00-00-E0", netmask=None, broadcast=None, ptp=None), snic(family=23, address="2001:0:9d38:6ab8:2817:d7f5:8b18:2afa", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::2817:d7f5:8b18:2afa", netmask=None, broadcast=None, ptp=None)], "isatap.{4055306E-39BD-4FD5-9BF8-72E03E28110C}": [snic(family=-1, address="00-00-00-00-00-00-00-E0", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.1.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.2.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.3.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.4.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.5.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.6.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.7.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.8.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.20.45", netmask=None, broadcast=None, ptp=None), snic(family=23, address="fe80::5efe:192.168.100.45", netmask=None, broadcast=None, ptp=None)], "本地连接": [snic(family=-1, address="B8-2A-72-B0-AD-49", netmask=None, broadcast=None, ptp=None), snic(family=2, address="192.168.1.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.2.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.3.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.4.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.5.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.6.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.7.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.8.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.20.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=2, address="192.168.100.45", netmask="255.255.255.0", broadcast=None, ptp=None), snic(family=23, address="fe80::7929:9a86:77f0:1157", netmask=None, broadcast=None, ptp=None)], "Loopback Pseudo-Interface 1": [snic(family=2, address="127.0.0.1", netmask="255.0.0.0", broadcast=None, ptp=None), snic(family=23, address="::1", netmask=None, broadcast=None, ptp=None)]}
>>>
>>> >>> psutil.net_if_stats()
{u"Teredo Tunneling Pseudo-Interface": snicstats(isup=True, duplex=2, speed=0, mtu=1472),

u"isatap.{4055306E-39BD-4FD5-9BF8-72E03E28110C}": snicstats(isup=False, duplex=2, speed=0, mtu=1280),

u"Loopback Pseudo-Interface 1": snicstats(isup=True, duplex=2, speed=1073, mtu=1500),

"本地连接": snicstats(isup=True, duplex=2, speed=100, mtu=1500),

u"u672cu5730u8fdeu63a5": (True, 2, 100, 1500)}
>>>

2.5进程管理

>>> psutil.pids()
[0, 4, 312, 512, 564, 576, 624, 648, 660, 676, 776, 856, 956, 1000, 268, 392, 1080, 1104, 1112, 1140, 1188, 1616, 1644, 1756, 1776, 1784, 1944, 1968, 2004, 1064, 1240, 1680, 2176, 2268, 2316, 2408, 2444, 2684, 2760, 2784, 2828, 2880, 3264, 3368, 3716, 3808, 3904, 3940, 4068, 3672, 4516, 4532, 4540, 4548, 4556, 4640, 5000, 560, 1452, 1504, 5108, 5572, 5596, 5552, 5040, 6060, 6164, 6312, 3156, 6684, 6100, 5340, 5720, 5600, 6728, 3212, 400, 3124, 6080, 7156]
>>> p=psutil.Process(5600)
>>> p.name()
"conhost.exe"
>>> p.username()
u"PC-TEST\Administrator"
>>> p.cmdline()
["\??\C:\windows\system32\conhost.exe", "19633873301273583272-200579322-233405410535233736-1237920629-4612726532082004861"]
>>> p.cwd
<bound method Process.cwd of <psutil.Process(pid=5600, name="conhost.exe") at 43361328>>

运行进程所在的目录

>>> p.cwd()
"C:\windows\system32"
>>> p.exe()
"C:\Windows\System32\conhost.exe"
>>> p.cpu_affinity()
[0, 1, 2, 3]
>>> p.pid
5600

父进程pid

>>> p.ppid()
576

返回父进程,如果不存在返回None

>>> p.parent()
<psutil.Process(pid=576, name="csrss.exe") at 43438448>
>>> p.children()
[]
>>> p.num_threads()
1
>>> p.threads()
[pthread(id=2772, user_time=0.0, system_time=0.015600099999999999)]
>>> p.status()
"running"
>>> p.is_running()
True
>>> p.suspend()
>>> p.status()
"stopped"
>>> p.is_running()
True
>>> p.resume()
>>> p.status()
"running"
>>> p.kill()
>>> psutil.pid_exists(5600)
False
>>> psutil.pid_exists(6060)
True
>>> psutil.test()
USER         PID %MEM     VSZ     RSS TTY           START    TIME  COMMAND
SYSTEM         0    ?       ?      24 ?             09:38   35:20  System Idle Process
SYSTEM         4    ?     120     368 ?             09:38   04:31  System
             268  0.5    9424   19844 ?             09:38   00:05  svchost.exe
             312    ?     548    1016 ?             09:38   00:00  smss.exe
             392  1.1   30208   43144 ?             09:38   01:51  svchost.exe
Administra   400  1.2   32788   47180 ?             13:27   00:37  pythonw.exe
             512  0.1    2624    5384 ?             09:38   00:08  csrss.exe
Administra   560  0.7   14036   28392 ?             09:40   01:00  RaUI.exe
             564  0.1    1688    4636 ?             09:38   00:00  wininit.exe
             576  1.3    3696   54796 ?             09:38   00:35  csrss.exe
             624  0.3    8240   10804 ?             09:38   32:52  services.exe
             648  0.3    5360   11788 ?             09:38   00:20  lsass.exe
             660  0.1    3116    6128 ?             09:38   00:00  lsm.exe
             676  0.2    3392    6780 ?             09:38   00:00  winlogon.exe
             776  0.2    4896    9040 ?             09:38   03:09  svchost.exe
             856  0.2    5264    9444 ?             09:38   00:05  svchost.exe
             956  0.5   25040   20664 ?             09:38   00:28  svchost.exe
            1000  3.7  144452  151748 ?             09:38   01:43  svchost.exe
            1064  0.1    2380    5544 ?             09:38   00:00  lmgrd.exe
            1080  0.1    2220    5160 ?             09:38   00:00  RtkAudioService64.exe
            1104  0.2   15132    9080 ?             09:38   00:01  RAVBg64.exe
            1112  0.2   14672    9104 ?             09:38   00:01  RAVBg64.exe
            1140  0.5   21172   19780 ?             09:38   00:09  ZhuDongFangYu.exe
            1188  0.4   15920   17480 ?             09:38   00:24  svchost.exe
            1240  0.5   73020   20316 ?             09:38   00:01  MsDtsSrvr.exe
            1452  0.2    2580    6328 ?             09:40   00:05  svchost.exe
            1504  1.0   47656   39108 ?             09:40   00:28  SearchIndexer.exe
            1616  0.3    8536   12384 ?             09:38   00:01  spoolsv.exe
            1644  0.3    7080   11816 ?             09:38   00:07  svchost.exe
            1680  0.1    2568    5568 ?             09:38   00:00  ixialm.exe
            1756  0.1    1972    4428 ?             09:38   00:00  lmgrd.exe
            1776  0.1    1456    4220 ?             09:38   00:00  conhost.exe
            1784  0.4   16576   15356 ?             09:38   00:06  noded.exe
            1944  0.1    1448    2804 ?             09:38   00:00  AERTSr64.exe
            1968  0.2    5004    7500 ?             09:38   00:01  svchost.exe
            2004  0.1    3280    5672 ?             09:38   00:00  endpoint.exe
            2176  0.2    5268    7096 ?             09:38   00:00  mqsvc.exe
            2268  0.9  129344   37576 ?             09:38   00:02  sqlservr.exe
            2316  0.6   45048   23888 ?             09:39   00:02  msmdsrv.exe
            2356  0.5   19780   21108 ?             13:52   00:00  audiodg.exe
ntp         2408  0.1    1864    4824 ?             09:39   00:00  ntpd.exe
            2444  0.6   25072   24352 ?             09:39   00:03  QQProtect.exe
            2684  0.1    1860    5672 ?             09:39   00:00  RaRegistry.exe
            2760  0.1    1840    3880 ?             09:39   00:00  RaRegistry64.exe
            2784  0.1    2172    5644 ?             09:39   00:01  svchost.exe
            2828  0.3    5460   12528 ?             09:39   00:02  TeamViewer_Service.exe
            2880  0.2    6304    8064 ?             09:39   00:01  svchost.exe
Administra  3124  0.4   11836   16588 ?             13:29   00:13  pythonw.exe
Administra  3156  1.1   25152   47104 ?             11:14   00:07  notepad++.exe
            3264  0.1    2168    4956 ?             09:39   00:01  svchost.exe
            3368  0.2    2380    6240 ?             09:39   00:03  svchost.exe
Administra  3672  0.9  167864   35248 ?             09:39   01:00  360tray.exe
Administra  3716  0.4   13568   16204 ?             09:39   00:01  taskhost.exe
Administra  3808  0.7   38116   30500 ?             09:39   05:37  dwm.exe
            3904  0.4   11248   17236 ?             09:39   07:17  WmiPrvSE.exe
            3940  0.2    2956    7144 ?             09:39   00:00  WmiPrvSE.exe
Administra  4068  3.9   98944  158780 ?             09:39   04:38  explorer.exe
Administra  4516  0.4   19316   16116 ?             09:40   00:01  RtkNGUI64.exe
Administra  4532  0.4   22072   16888 ?             09:40   00:01  RAVBg64.exe
Administra  4540  0.1    1864    4584 ?             09:40   00:00  WavesSvc64.exe
Administra  4548  0.5   14104   20000 ?             09:40   00:02  SynTPEnh.exe
Administra  4556  1.0   25664   40096 ?             09:40   00:01  MySQLNotifier.exe
Administra  4640  0.3    6924   10868 ?             09:40   00:01  iusb3mon.exe
Administra  5000  0.3    7148   11212 ?             09:40   00:00  SynTPHelper.exe
Administra  5040  1.0   28440   42436 ?             09:48   00:00  Foxmail.exe
            5108  1.2  158128   50152 ?             09:41   10:55  svchost.exe
Administra  5340 14.7  545928  600200 ?             13:26   05:40  firefox.exe
Administra  5552  1.2   36364   48600 ?             09:48   00:37  Foxmail.exe
Administra  5572  0.5   11348   21128 ?             09:42   01:08  SogouCloud.exe
Administra  5596  1.0   64296   38936 ?             09:48   00:40  Foxmail.exe
Administra  5720  0.1     884    3224 ?             13:26   00:00  helper.exe
Administra  6060  0.8   23800   33992 ?             09:50   00:00  Foxmail.exe
Administra  6164  4.5  127868  182852 ?             09:51   01:41  QQ.exe
Administra  6312  0.1    2720    2280 ?             09:51   00:00  TXPlatform.exe
Administra  6728  1.2   38084   49380 ?             13:26   00:04  plugin-container.exe

2.6其他信息

获取登录用户

>>> psutil.users()
[suser(name="Administrator", terminal=None, host="0.0.0.0", started=1501810725.0)]

获取开机时间

>>> psutil.boot_time()
1501810706.0

>>> import datetime
>>> datetime.datetime.fromtimestamp(psutil.boot_time())
datetime.datetime(2017, 8, 4, 9, 38, 26)
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
"2017-08-04 09:38:26"
>>>


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。