這篇文章主要給大家介紹了關於Python中psutil的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
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('---------------------')
如果你不想每次都運行這腳本,可以使用pyinstaller打包,教程可以參考前面的文章。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。