Python读取Windows硬件与系统信息实现指南
本文介绍使用Python读取Windows机器硬件及系统信息的方法。通过调用win32api和_winreg模块从注册表获取操作系统名称、版本号及型号,利用GetFileVersionInfo解析文件版本。同时结合platform模块获取架构、主机名、处理器及网络地址等详细系统参数,提供完整的代码实现与输出示例。
本文详细介绍一种使用Python获取计算机硬件及系统信息的具体实现方案,供相关开发者参考与借鉴。
笔者在近期学习Python过程中,涉及获取机器相关信息的实际需求。经过对网络资料的搜集、整理与实测验证,总结出一套可行的操作方法。
# -*- coding: UTF8 -*- import os import win32api import datetime import platform import getpass import socket import uuid import _winreg import re
1、通过读取注册表获取操作系统名称
def GetOsName():
'''操作系统名称'''
keyPath = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
each_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyPath, 0, _winreg.KEY_READ)
OsName,REG_SZ = _winreg.QueryValueEx(each_key, "ProductName")
return OsName
2、通过读取注册表获取操作系统当前版本号
def GetOsVersion():
'''操作系统版本'''
keyPath = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
each_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyPath, 0, _winreg.KEY_READ)
OsVersion,REG_SZ = _winreg.QueryValueEx(each_key, "CurrentVersion")
return OsVersion
3、通过读取注册表获取操作系统型号
def GetOsModel():
'''操作系统型号'''
keyPath = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
each_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyPath, 0, _winreg.KEY_READ)
OsModel,REG_SZ = _winreg.QueryValueEx(each_key, "EditionID")
return OsModel
4、依据文件全路径获取该文件的版本号
def GetFileVersion(filePath):
info = win32api.GetFileVersionInfo(filePath, os.sep)
ms = info['FileVersionMS']
ls = info['FileVersionLS']
version = '%d.%d.%d.%04d' % (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))
return version
5、利用platform模块读取机器的其他详细信息
def get_architecture():
'''获取操作系统的位数'''
return platform.architecture()
def get_machine():
'''计算机类型'''
return platform.machine()
def get_node():
'''计算机的网络名称'''
return platform.node()
def get_processor():
'''计算机处理器信息'''
return platform.processor()
def get_system():
'''获取操作系统类型'''
return platform.system()
def get_TotalInfo():
'''汇总信息'''
return platform.uname()
def get_localDataPath():
'''当前用户路径'''
return os.path.expanduser('~')
def get_UserName():
'''当前用户名'''
return getpass.getuser()
def get_ComputerName1():
'''获取机器名称'''
return platform.node()()
def get_ComputerName():
'''获取机器名称'''
return socket.gethostname()
def get_AddressIp():
'''获取本机IP'''
return socket.gethostbyname(get_ComputerName())
def get_Mac():
'''获取MAC地址'''
mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
return ':'.join(mac[e:e+2].upper() for e in xrange(0,11,2))
def show_os_all_info():
'''打印os的全部信息'''
print('操作系统的位数 : [{}]'.format(get_architecture()))
print('计算机类型 : [{}]'.format(get_machine()))
print('计算机的网络名称 : [{}]'.format(get_node()))
print('计算机处理器信息 : [{}]'.format(get_processor()))
print('操作系统类型 : [{}]'.format(get_system()))
print('汇总信息 : [{}]'.format(get_TotalInfo()))
print('当前用户路径: [{}]'.format(get_localDataPath()))
print('当前用户名: [{}]'.format(get_UserName()))
print('机器名称: [{}]'.format(get_ComputerName()))
print('机器IP: [{}]'.format(get_AddressIp()))
print('MAC地址: [{}]'.format(get_Mac()))
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述内容能对读者的Python程序设计工作有所帮助。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













