温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Python中使用psutil方法

发布时间:2021-04-01 17:58:13 来源:亿速云 阅读:229 作者:Leah 栏目:开发技术

如何在Python中使用psutil方法?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

psutil简介

psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统.

如果安装了Anaconda,psutil就可以使用,当然也可使用pip安装,使用前先要导包:

import psutil

主要方法简介

psutil.disk_partitions()返回是一个磁盘分区信息,包括(device, mountpoint, fstype, opts);

psutil.disk_usage()返回磁盘使用情况:

disk = psutil.disk_partitions()
for i in disk:
 print("磁盘:%s 分区格式:%s" % (i.device, i.fstype)) # 盘符 分区格式
 disk_use = psutil.disk_usage(i.device) 

 print("使用了:%.1f GB,空闲:%.1f GB,总共:%.1f GB,使用率%.1f%%," % (
 disk_use.used / 1024 / 1024 / 1024, disk_use.free / 1024 / 1024 / 1024, disk_use.total / 1024 / 1024 / 1024,
 disk_use.percent))

磁盘:C:\   分区格式:NTFS
使用了:34.8 GB,空闲:48.2GB,总共:83.0 GB,使用率41.9%,
磁盘:D:\   分区格式:NTFS
使用了:110.5 GB,空闲:89.2GB,总共:199.7 GB,使用率55.4%,
磁盘:E:\   分区格式:NTFS
使用了:100.1 GB,空闲:95.2GB,总共:195.3 GB,使用率51.3%,
磁盘:F:\   分区格式:NTFS
使用了:120.6 GB,空闲:64.4GB,总共:184.9 GB,使用率65.2%, 

psutil.cpu_percent() cpu的利用率

psutil.virtual_memory()内存情况

memory = psutil.virtual_memory()
# memory.used 使用的
# memory.total 总共
ab = float(memory.used) / float(memory.total) * 100
print("内存使用率为:%.2f%%" % ab)

psutil.net_io_counters() 网络使用情况,可以监控电脑每一个网口的上传,下载等信息;每个电脑由于网口名字不同,返回的信息不太一样。用下面的代码可以先打印出来你电脑的网口信息:

print(psutil.net_io_counters(pernic=True))

你会得到类型下面的信息:

{'以太网': 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=597, bytes_recv=13, packets_sent=596, packets_recv=13, errin=0, errout=0, dropin=0, dropout=0), 'VMware Network Adapter VMnet8': snetio(bytes_sent=1919, bytes_recv=13, packets_sent=1919, packets_recv=13, errin=0, errout=0, dropin=0, dropout=0), 'WLAN': snetio(bytes_sent=3993804, bytes_recv=76316885, packets_sent=35011, packets_recv=63467, 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), 'Teredo Tunneling Pseudo-Interface': snetio(bytes_sent=13724, bytes_recv=760, packets_sent=102, packets_recv=5, errin=0, errout=0, dropin=0, dropout=0)}

现在你就可以愉快的监控宽带或WLAN的上传和下载速度了,代码如下:

import psutil
import time

def net_state():
 
 recv1 = psutil.net_io_counters(pernic=True)['WLAN'][1] #接收数据
 send1 = psutil.net_io_counters(pernic=True)['WLAN'][0] #上传数据
 time.sleep(1) # 每隔1s监听端口接收数据
 recv2 = psutil.net_io_counters(pernic=True)['WLAN'][1]
 send2 = psutil.net_io_counters(pernic=True)['WLAN'][0]
 # 上传数据
 return 'upload:%.1f kb/s.' % ((send2 - send1) / 1024.0), 'download:%.1f kb/s.' % ((recv2 - recv1) / 1024.0)

while True:
 s1 = net_state()[0]
 s2 = net_state()[1]
 print('当前上传和下载速度为:')
 print(s1)
 print(s2)
 print('---------------------')

看完上述内容,你们掌握如何在Python中使用psutil方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI