这一切的传说都不足以描述他们的真面目。当她现身人类面前时,远比任何传说都更加狰狞和威严。
psutil库是python获取计算机CPU,内存,网络,磁盘数据的一个库。并不是自带的🔋库,需要手动安装。
获取当前用户
print('当前用户:{}'.format(psutil.users()[0][0]))
>>>当前用户:langzi
获取CPU个数
print('CPU逻辑个数:{}'.format(psutil.cpu_count()))
print('CPU物理个数:{}'.format(psutil.cpu_count(logical=False)))
>>>CPU逻辑个数:16
>>>CPU物理个数:8
检测内存总量
mem=psutil.virtual_memory().total/1024/1024/1000
print('总内存: {} G'.format(str(mem).split('.')[0]))
>>>总内存: 32 G
检测可用内存
mem=psutil.virtual_memory().available/1024/1024/1000
print('可用内存 : {} G'.format(str(mem).split('.')[0]))
>>>可用内存 : 22 G
检测已使用内存
mem=psutil.virtual_memory().used/1024/1024/1000
print('已用内存:{}G'.format(str(mem).split('.')[0]))
>>>已用内存:10G
检测内存使用的比例
print('已用内存:{} %'.format( psutil.virtual_memory().percent) )
>>>已用内存:31.3%
查看磁盘空间使用情况
disk=psutil.disk_partitions()
for i in disk:
print(i)
>>>>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='G:\\', mountpoint='G:\\', fstype='', opts='cdrom')
sdiskpart(device='L:\\', mountpoint='L:\\', fstype='exFAT', opts='rw,fixed')
查看每个网卡的状态
for k,v in psutil.net_io_counters(pernic=True).items():
print(k,':',v)
>>>VirtualBox Host-Only Network : snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)
以太网 3 : snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)
VirtualBox Host-Only Network #2 : 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=0, bytes_recv=0, packets_sent=0, packets_recv=0, 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)
以太网 : snetio(bytes_sent=13115767, bytes_recv=303119450, packets_sent=117310, packets_recv=228972, errin=0, errout=0, dropin=0, dropout=0)
VMware Network Adapter VMnet1 : snetio(bytes_sent=945, bytes_recv=12, packets_sent=945, packets_recv=12, errin=0, errout=0, dropin=0, dropout=0)
VMware Network Adapter VMnet8 : snetio(bytes_sent=2093, bytes_recv=972, packets_sent=2093, packets_recv=972, 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)
获取一小时内宽带流量/CPU使用率
import psutil,time
def network():
p = psutil
before_recv = p.net_io_counters().bytes_recv
before_send = p.net_io_counters().bytes_sent
time.sleep(3600)
# 每个小时
now_recv = p.net_io_counters().bytes_recv
now_send = p.net_io_counters().bytes_sent
delta_send = (now_send - before_send) / 1024000
delta_recv = (now_recv - before_recv) / 1024000
return (int(delta_send),int(delta_recv))
def Cor():
# 返回一个小时内,CPU/内存 使用率% 和使用的宽带 上传/下载量 M
new = network()
# 返回 ('25.7%', '2.3%', '0Mb', '0Mb')
return (str(psutil.virtual_memory().percent)+'%',str(psutil.cpu_percent(True))+'%',str(new[0])+'Mb',str(new[1])+'Mb')
if __name__ == '__main__':
print('内存使用率:{}%'.format(psutil.virtual_memory().percent))
print('CPU使用率:{}%'.format(psutil.cpu_percent(True)))
new = network()
print('宽带上传量:{}MB'.format(new[0]))
print('宽带下载量:{}MB'.format(new[1]))