Python WMI获取Windows系统信息
2021/4/23 7:28:56
本文主要是介绍Python WMI获取Windows系统信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 #http://www.cnblogs.com/liu-ke/ 4 import wmi 5 import os 6 import sys 7 import platform 8 import time 9 10 def sys_version(): 11 c = wmi.WMI () 12 #获取操作系统版本 13 for sys in c.Win32_OperatingSystem(): 14 print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber 15 print sys.OSArchitecture.encode("UTF8")#系统是32位还是64位的 16 print sys.NumberOfProcesses #当前系统运行的进程总数17 18 def cpu_mem(): 19 c = wmi.WMI () 20 #CPU类型和内存 21 for processor in c.Win32_Processor(): 22 #print "Processor ID: %s" % processor.DeviceID 23 print "Process Name: %s" % processor.Name.strip() 24 for Memory in c.Win32_PhysicalMemory(): 25 print "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576) 26 27 def cpu_use(): 28 #5s取一次CPU的使用率 29 c = wmi.WMI() 30 while True: 31 for cpu in c.Win32_Processor(): 32 timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) 33 print '%s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage) 34 time.sleep(5) 35 36 def disk(): 37 c = wmi.WMI () 38 #获取硬盘分区 39 for physical_disk in c.Win32_DiskDrive (): 40 for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): 41 for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"): 42 print physical_disk.Caption.encode("UTF8"), partition.Caption.encode("UTF8"), logical_disk.Caption 43 44 #获取硬盘使用百分情况 45 for disk in c.Win32_LogicalDisk (DriveType=3): 46 print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size)) 47 48 def network(): 49 c = wmi.WMI () 50 #获取MAC和IP地址 51 for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 52 print "MAC: %s" % interface.MACAddress 53 for ip_address in interface.IPAddress: 54 print "ip_add: %s" % ip_address 55 print 56 57 #获取自启动程序的位置 58 for s in c.Win32_StartupCommand (): 59 print "[%s] %s <%s>" % (s.Location.encode("UTF8"), s.Caption.encode("UTF8"), s.Command.encode("UTF8")) 60 61 62 #获取当前运行的进程 63 for process in c.Win32_Process (): 64 print process.ProcessId, process.Name 65 66 def main(): 67 sys_version() 68 cpu_mem() 69 #disk() 70 #network() 71 #cpu_use() 72 73 if __name__ == '__main__': 74 main() 75 print platform.system() 76 print platform.release() 77 print platform.version() 78 print platform.platform() 79 print platform.machine()
这篇关于Python WMI获取Windows系统信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程