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


Posted in Python onMay 15, 2015

本文实例讲述了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 相关文章推荐
巧用Python装饰器 免去调用父类构造函数的麻烦
May 18 Python
python模拟新浪微博登陆功能(新浪微博爬虫)
Dec 24 Python
python爬虫教程之爬取百度贴吧并下载的示例
Mar 07 Python
python实现用户登录系统
May 21 Python
Python 正则表达式的高级用法
Dec 04 Python
Python排序算法实例代码
Aug 10 Python
python K近邻算法的kd树实现
Sep 06 Python
Python类装饰器实现方法详解
Dec 21 Python
Python正则匹配判断手机号是否合法的方法
Dec 09 Python
Python实现 PS 图像调整中的亮度调整
Jun 28 Python
python中利用matplotlib读取灰度图的例子
Dec 07 Python
使用Python通过企业微信应用给企业成员发消息
Apr 18 Python
python获取当前时间对应unix时间戳的方法
May 15 #Python
Python加pyGame实现的简单拼图游戏实例
May 15 #Python
Python实现从URL地址提取文件名的方法
May 15 #Python
Python基础入门之seed()方法的使用
May 15 #Python
Python中的random()方法的使用介绍
May 15 #Python
Python的randrange()方法使用教程
May 15 #Python
Python中的choice()方法使用详解
May 15 #Python
You might like
利用PHP制作简单的内容采集器的原理分析
2008/10/01 PHP
PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】
2009/10/11 PHP
PHP封装的Twitter访问类实例
2015/07/18 PHP
详解 PHP加密解密字符串函数附源码下载
2015/12/18 PHP
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
2015/12/21 PHP
PHP实现基于mysqli的Model基类完整实例
2016/04/08 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
ThinkPHP实现的rsa非对称加密类示例
2018/05/29 PHP
关于javascript 回调函数中变量作用域的讨论
2009/09/11 Javascript
JS中引用百度地图并将百度地图的logo和信息去掉
2013/09/29 Javascript
jquery实现鼠标拖动图片效果示例代码
2014/01/09 Javascript
js+html5实现canvas绘制镂空字体文本的方法
2015/06/05 Javascript
基于jQuery通过jQuery.form.js插件实现异步上传
2015/12/13 Javascript
JS事件添加和移出的兼容写法示例
2016/06/20 Javascript
jQuery UI制作选项卡(tabs)
2016/12/13 Javascript
vue.js获取数据库数据实例代码
2017/05/26 Javascript
angularjs路由传值$routeParams详解
2020/09/05 Javascript
jQuery第一次运行页面默认触发点击事件的实例
2018/01/10 jQuery
微信小程序wx.getImageInfo()如何获取图片信息
2018/01/26 Javascript
JavaScript 斐波那契数列 倒序输出 输出100以内的质数代码实例
2019/09/11 Javascript
何时/使用 Vue3 render 函数的教程详解
2020/07/25 Javascript
[46:57]EG vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
深入浅析ImageMagick命令执行漏洞
2016/10/11 Python
深入理解Python3中的http.client模块
2017/03/29 Python
pandas 把数据写入txt文件每行固定写入一定数量的值方法
2018/12/28 Python
python中的decimal类型转换实例详解
2019/06/26 Python
python如何实现代码检查
2019/06/28 Python
python实现将中文日期转换为数字日期
2020/07/14 Python
BNKR中国官网:带你感受澳洲领先潮流时尚
2018/08/21 全球购物
戴森比利时官方网站:Dyson BE
2020/10/03 全球购物
高中生毕业自我鉴定范文
2013/12/22 职场文书
写给女朋友的检讨书
2014/01/28 职场文书
市场调查策划方案
2014/06/10 职场文书
司法局2014法制宣传日活动总结
2014/11/01 职场文书
2014年销售员工作总结
2014/12/01 职场文书
Python自动化之批量处理工作簿和工作表
2021/06/03 Python