python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法

 更新时间:2015年05月15日 15:21:03   作者:令狐不聪  
这篇文章主要介绍了python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法,涉及Python针对系统信息的相关操作技巧,需要的朋友可以参考下

本文实例讲述了python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import wmi 
import sys,time,platform 
def get_system_info(os): 
  """ 
  获取操作系统版本。 
  """ 
  print 
  print "Operating system:" 
  if os == "Windows": 
    c = wmi.WMI () 
    for sys in c.Win32_OperatingSystem(): 
      print '\t' + "Version :\t%s" % sys.Caption.encode("GBK") 
      print '\t' + "Vernum :\t%s" % sys.BuildNumber 
def get_memory_info(os): 
  """ 
  获取物理内存和虚拟内存。 
  """ 
  print 
  print "memory_info:" 
  if os == "Windows": 
    c = wmi.WMI () 
    cs = c.Win32_ComputerSystem() 
    pfu = c.Win32_PageFileUsage() 
    MemTotal = int(cs[0].TotalPhysicalMemory)/1024/1024 
    print '\t' + "TotalPhysicalMemory :" + '\t' + str(MemTotal) + "M" 
    #tmpdict["MemFree"] = int(os[0].FreePhysicalMemory)/1024 
    SwapTotal = int(pfu[0].AllocatedBaseSize) 
    print '\t' + "SwapTotal :" + '\t' + str(SwapTotal) + "M" 
    #tmpdict["SwapFree"] = int(pfu[0].AllocatedBaseSize - pfu[0].CurrentUsage) 
def get_disk_info(os): 
  """ 
  获取物理磁盘信息。 
  """ 
  print 
  print "disk_info:" 
  if os == "Windows": 
    tmplist = [] 
    c = wmi.WMI () 
    for physical_disk in c.Win32_DiskDrive(): 
      if physical_disk.Size: 
        print '\t' + str(physical_disk.Caption) + ' :\t' + str(long(physical_disk.Size)/1024/1024/1024) + "G" 
def get_cpu_info(os): 
  """ 
  获取CPU信息。 
  """ 
  print 
  print "cpu_info:" 
  if os == "Windows": 
    tmpdict = {} 
    tmpdict["CpuCores"] = 0 
    c = wmi.WMI () 
    for cpu in c.Win32_Processor():       
      tmpdict["CpuType"] = cpu.Name 
    try: 
      tmpdict["CpuCores"] = cpu.NumberOfCores 
    except: 
      tmpdict["CpuCores"] += 1 
      tmpdict["CpuClock"] = cpu.MaxClockSpeed   
    print '\t' + 'CpuType :\t' + str(tmpdict["CpuType"]) 
    print '\t' + 'CpuCores :\t' + str(tmpdict["CpuCores"]) 
def get_network_info(os): 
  """ 
  获取网卡信息和当前TCP连接数。 
  """ 
  print 
  print "network_info:" 
  if os == "Windows": 
    tmplist = [] 
    c = wmi.WMI () 
    for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): 
        tmpdict = {} 
        tmpdict["Description"] = interface.Description 
        tmpdict["IPAddress"] = interface.IPAddress[0] 
        tmpdict["IPSubnet"] = interface.IPSubnet[0] 
        tmpdict["MAC"] = interface.MACAddress 
        tmplist.append(tmpdict) 
    for i in tmplist: 
      print '\t' + i["Description"] 
      print '\t' + '\t' + "MAC :" + '\t' + i["MAC"] 
      print '\t' + '\t' + "IPAddress :" + '\t' + i["IPAddress"] 
      print '\t' + '\t' + "IPSubnet :" + '\t' + i["IPSubnet"] 
    for interfacePerfTCP in c.Win32_PerfRawData_Tcpip_TCPv4(): 
        print '\t' + 'TCP Connect :\t' + str(interfacePerfTCP.ConnectionsEstablished) 
if __name__ == "__main__": 
  os = platform.system() 
  get_system_info(os) 
  get_memory_info(os) 
  get_disk_info(os) 
  get_cpu_info(os) 
  get_network_info(os) 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

  • Python语言中Tuple的由来分析

    Python语言中Tuple的由来分析

    Tuple在Python中表示一种“大小固定的有序序列”,这篇文章主要介绍了Python语言中Tuple的由来,需要的朋友可以参考下
    2022-09-09
  • 自学python的建议和周期预算

    自学python的建议和周期预算

    在本篇文章中小编给大家分享了关于自学python的建议和周期预算,有兴趣的朋友们可以学习参考下。
    2019-01-01
  • 用Python实现批量生成法务函代码

    用Python实现批量生成法务函代码

    大家好,本篇文章主要讲的是用Python实现批量生成法务函代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02
  • 利用Python第三方库实现预测NBA比赛结果

    利用Python第三方库实现预测NBA比赛结果

    今天给大家带来的是关于Python的相关知识,文章围绕着利用Python实现预测NBA比赛结果展开,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • Python实现手绘图效果实例分享

    Python实现手绘图效果实例分享

    在本篇文章里小编给大家整理了关于Python实现手绘图效果,有需要的朋友们可以学习下。
    2020-07-07
  • Python+OpenCV采集本地摄像头的视频

    Python+OpenCV采集本地摄像头的视频

    这篇文章主要为大家详细介绍了Python+OpenCV采集本地摄像头的视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Python线性回归图文实例详解

    Python线性回归图文实例详解

    用python进行线性回归分析非常方便,有现成的库可以使用比如numpy.linalog.lstsq、scipy.stats.linregress、pandas.ols等,这篇文章主要给大家介绍了关于Python线性回归的相关资料,需要的朋友可以参考下
    2021-11-11
  • pytorch tensor按广播赋值scatter_函数的用法

    pytorch tensor按广播赋值scatter_函数的用法

    这篇文章主要介绍了pytorch tensor按广播赋值scatter_函数的用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • python 导入数据及作图的实现

    python 导入数据及作图的实现

    今天小编就为大家分享一篇python 导入数据及作图的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • pytest中conftest.py使用小结

    pytest中conftest.py使用小结

    conftest.py文件是Pytest框架里面一个很重要的东西,本文主要介绍了pytest中conftest.py使用小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09

最新评论