Python用WMI模块获取windowns系统信息

简介:

    Python用WMI模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息。

 
  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  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() 

 


本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1158075,如需转载请自行联系原作者

相关文章
|
22天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
24天前
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
|
1天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
11 0
|
4天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
6天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
6天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
39 1
|
8天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
9天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
11天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
16天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
27 0

热门文章

最新文章