Python实现的读取电脑硬件信息功能示例

作者:shw800 时间:2023-05-13 21:36:03 

本文实例讲述了Python实现的读取电脑硬件信息功能。分享给大家供大家参考,具体如下:

上学那会,老师让我用java获取电脑硬件信息,CPU, 硬盘,MAC等,那个时候感觉搞了好久。。。。。。

今天,用python试了一下,简单多了。分享一下:

首先安装wmi库,wmi是一种规范和基础结构,通过它可以访问、配置、管理和监视几乎所有的Windows资源。大多用户习惯于使用众多的图形化管理工 具来管理Windows资源,在wmi之前这些工具都是通过 Win32应用程序编程接口来访问和管理Windows资源的。大多数脚本 语言都不能直接调用Win32 API,wmiI的出现使得系统管理员可以通过一种简便的方法即利用常见的脚本语言实现常用的系统管理任务。好了,上代码吧


import wmi
import time
import json
import win32com
class PCHardwork(object):
global s
s = wmi.WMI()
def get_CPU_info(self):
 cpu = []
 cp = s.Win32_Processor()
 for u in cp:
  cpu.append(
   {
    "Name": u.Name,
    "Serial Number": u.ProcessorId,
    "CoreNum": u.NumberOfCores,
    "numOfLogicalProcessors": u.NumberOfLogicalProcessors,
    "timestamp": time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()),
    "cpuPercent": u.loadPercentage
   }
  )
 print ":::CPU info:", json.dumps(cpu, True, indent=4)
 return cpu
def get_disk_info(self):
 disk = []
 for pd in s.Win32_DiskDrive():
  disk.append(
   {
    "Serial": s.Win32_PhysicalMedia()[0].SerialNumber.lstrip().rstrip(), # 获取硬盘序列号,调用另外一个win32 API
    "ID": 123456,
    "Caption": pd.Caption,
    "size": str(int(float(pd.Size)/1024/1024/1024))+"G"
   }
  )
 print":::Disk info:", json.dumps(disk, True, indent=4)
 return disk
def get_network_info(self):
 network = []
 for nw in s.Win32_NetworkAdapterConfiguration (IPEnabled=1):
  network.append(
   {
    "MAC": nw.MACAddress,
    "ip": nw.IPAddress
   }
  )
 print":::Network info:", json.dumps(network, True, indent=4)
 return network
def get_running_process(self):
 process = []
 for p in s.Win32_Process():
  process.append(
   {
    p.Name: p.ProcessId
   }
  )
 print":::Running process:", json.dumps(process, True, indent=4)
 return process
#运行测试:
PCinfo = PCHardwork()
PCinfo.get_CPU_info()
PCinfo.get_disk_info()
PCinfo.get_network_info()
PCinfo.get_running_process()

运行结果:

:::CPU info: [
    {
        "numOfLogicalProcessors": 2,
        "cpuPercent": 27,
        "Name": "Pentium(R) Dual-Core  CPU      E5300  @ 2.60GHz",
        "CoreNum": 2,
        "timestamp": "Tue, 29 May 2018 15:19:52",
        "Serial Number": "BFEBFBFF0001067A"
    }
]
:::Disk info: [
    {
        "Caption": "WDC WD5000AAKX-22ERMA0 ATA Device",
        "Serial": "WD-WCC2EV784095",
        "ID": 123456,
        "size": "465G"
    }
]
:::Network info: [
    {
        "ip": [
            "192.168.3.37",
            "fe80::d1a:8c98:b6d9:5f28"
        ],
        "MAC": "E0:CB:4E:07:75:85"
    }
]
:::Running process: [
    {
        "System Idle Process": 0
    },
    {
        "System": 4
    },
    {
        "smss.exe": 296
    },
    {
        "csrss.exe": 428
    },
    {
        "wininit.exe": 484
    },
    {
        "csrss.exe": 504
    },
    {
        "services.exe": 548
    },
    {
        "winlogon.exe": 580
    },
    {
        "lsass.exe": 588
    },
    {
        "lsm.exe": 596
    },
    {
        "svchost.exe": 732
    },
    {
        "svchost.exe": 812
    },
    {
        "svchost.exe": 872
    },
    {
        "svchost.exe": 936
    },
    {
        "svchost.exe": 996
    },
    {
        "audiodg.exe": 1076
    },
    {
        "svchost.exe": 1124
    },
    {
        "ZhuDongFangYu.exe": 1272
    },
    {
        "svchost.exe": 1300
    },
    {
        "spoolsv.exe": 1556
    },
    {
        "svchost.exe": 1584
    },
    {
        "360bpsvc.exe": 1656
    },
    {
        "AlibabaProtect.exe": 1680
    },
    {
        "QQProtect.exe": 1800
    },
    {
        "secbizsrv.exe": 1860
    },
    {
        "TBSecSvc.exe": 1904
    },
    {
        "httpd.exe": 2004
    },
    {
        "mysqld.exe": 2040
    },
    {
        "wwbizsrv.exe": 456
    },
    {
        "taskhost.exe": 1376
    },
    {
        "dwm.exe": 2140
    },
    {
        "explorer.exe": 2156
    },
    {
        "TaobaoProtect.exe": 2368
    },
    {
        "360tray.exe": 2476
    },
    {
        "tomcat6w.exe": 2500
    },
    {
        "httpd.exe": 2676
    },
    {
        "360sd.exe": 2712
    },
    {
        "aliwssv.exe": 3084
    },
    {
        "conhost.exe": 3096
    },
    {
        "360bdoctor.exe": 3280
    },
    {
        "baidupinyin.exe": 3376
    },
    {
        "svchost.exe": 3420
    },
    {
        "360rp.exe": 4008
    },
    {
        "QQ.exe": 4136
    },
    {
        "TXPlatform.exe": 4480
    },
    {
        "360se.exe": 4624
    },
    {
        "360se.exe": 4792
    },
    {
        "wdswfsafe.exe": 4924
    },
    {
        "360se.exe": 5916
    },
    {
        "360se.exe": 4456
    },
    {
        "360se.exe": 4604
    },
    {
        "SoftMgrLite.exe": 2304
    },
    {
        "360se.exe": 5612
    },
    {
        "360se.exe": 2756
    },
    {
        "hh.exe": 6752
    },
    {
        "KMPlayer.exe": 7616
    },
    {
        "chrome.exe": 6848
    },
    {
        "chrome.exe": 7956
    },
    {
        "chrome.exe": 7044
    },
    {
        "chrome.exe": 6432
    },
    {
        "chrome.exe": 4396
    },
    {
        "chrome.exe": 5240
    },
    {
        "360se.exe": 968
    },
    {
        "TTPlayer.exe": 8636
    },
    {
        "360se.exe": 7080
    },
    {
        "chrome.exe": 8784
    },
    {
        "360se.exe": 7148
    },
    {
        "360se.exe": 8760
    },
    {
        "360se.exe": 2604
    },
    {
        "360se.exe": 4784
    },
    {
        "360se.exe": 8804
    },
    {
        "360se.exe": 7096
    },
    {
        "360se.exe": 8900
    },
    {
        "TrustedInstaller.exe": 5676
    },
    {
        "360se.exe": 5232
    },
    {
        "flashfxp.exe": 9356
    },
    {
        "flashfxp.exe": 4416
    },
    {
        "360se.exe": 9868
    },
    {
        "360se.exe": 8816
    },
    {
        "360se.exe": 6816
    },
    {
        "eclipsePHP.exe": 11000
    },
    {
        "javaw.exe": 9324
    },
    {
        "360se.exe": 11112
    },
    {
        "editplus.exe": 6748
    },
    {
        "cmd.exe": 11740
    },
    {
        "conhost.exe": 10532
    },
    {
        "eclipse.exe": 10556
    },
    {
        "python2.exe": 11772
    },
    {
        "conhost.exe": 3676
    },
    {
        "360se.exe": 9604
    },
    {
        "360se.exe": 10656
    },
    {
        "svchost.exe": 10888
    },
    {
        "WmiPrvSE.exe": 11968
    },
    {
        "WmiPrvSE.exe": 10988
    },
    {
        "python2.exe": 10956
    },
    {
        "conhost.exe": 10328
    }
]

简单吧,附上wmi api说明: https://msdn.microsoft.com/en-us/library/bg126473%28v=vs.85%29.aspx

补充:这里使用Python2.7平台测试,可能会出现如下错误:

1. no module named wmi 错误

可使用pip命令解决:


pip install wmi

即可。

2. no module named win32com.client 错误

本站下载pywin32-223-cp27-none-win32.whl 。

使用如下命令安装:


pip install pywin32-223-cp27-none-win32.whl

即可。

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

来源:https://blog.csdn.net/SHW800/article/details/46439717

标签:Python,电脑硬件信息
0
投稿

猜你喜欢

  • python实现登录密码重置简易操作代码

    2022-12-08 13:34:43
  • ASP数据库连接方式大全

    2023-07-12 05:52:59
  • 利用Opencv中Houghline方法实现直线检测

    2023-09-07 12:40:39
  • Python中规范定义命名空间的一些建议

    2022-05-16 17:05:24
  • 框架布局慎用元素

    2008-12-21 16:33:00
  • MySQL将select结果执行update的实例教程

    2024-01-17 14:00:59
  • 浅析Python中变量用法

    2023-08-08 02:02:31
  • mysql exists与not exists实例详解

    2024-01-24 22:57:14
  • mysql 8.0.14 安装配置方法图文教程

    2024-01-22 10:28:08
  • 朴素贝叶斯算法的python实现方法

    2023-03-01 07:05:53
  • 多种方式实现js图片预览

    2024-04-23 09:24:44
  • python 如何调用远程接口

    2022-05-09 20:59:59
  • python 一篇文章搞懂装饰器所有用法(建议收藏)

    2023-12-06 17:27:43
  • Python利用物理引擎Pymunk编写一个解压小游戏

    2022-03-08 16:15:45
  • 详解python中的闭包

    2023-09-25 13:37:33
  • 新手必备的Python实用技巧和工具

    2022-08-20 04:59:01
  • Numpy数据类型转换astype,dtype的方法

    2021-02-15 00:58:14
  • 在MySQL中使用Sphinx实现多线程搜索的方法

    2024-01-22 17:25:45
  • Sql Server查询性能优化之不可小觑的书签查找介绍

    2012-05-22 18:24:53
  • 利用python为PostgreSQL的表自动添加分区

    2023-07-07 14:44:58
  • asp之家 网络编程 m.aspxhome.com