国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現的讀取電腦硬件信息功能示例

2020-02-15 21:33:52
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現的讀取電腦硬件信息功能。分享給大家供大家參考,具體如下:

上學那會,老師讓我用java獲取電腦硬件信息,CPU, 硬盤,MAC等,那個時候感覺搞了好久。。。。。。

今天,用python試了一下,簡單多了。分享一下:

首先安裝wmi庫,wmi是一種規范和基礎結構,通過它可以訪問、配置、管理和監視幾乎所有的Windows資源。大多用戶習慣于使用眾多的圖形化管理工 具來管理Windows資源,在wmi之前這些工具都是通過 Win32應用程序編程接口來訪問和管理Windows資源的。大多數腳本 語言都不能直接調用Win32 API,wmiI的出現使得系統管理員可以通過一種簡便的方法即利用常見的腳本語言實現常用的系統管理任務。好了,上代碼吧

import wmiimport timeimport jsonimport win32comclass 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"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城步| 卫辉市| 浦江县| 禄丰县| 监利县| 镇远县| 石河子市| 文登市| 梁山县| 商城县| 景德镇市| 鹿邑县| 健康| 左贡县| 遂川县| 滁州市| 台湾省| 富平县| 义乌市| 祁阳县| 民和| 凭祥市| 临泉县| 怀柔区| 衢州市| 荣成市| 郓城县| 平潭县| 松阳县| 聂拉木县| 海口市| 古蔺县| 永川市| 广汉市| 瑞金市| 旬邑县| 龙游县| 庆云县| 怀集县| 乌鲁木齐市| 金塔县|