本文實例講述了Python實現讀取機器硬件信息的方法。分享給大家供大家參考,具體如下:
本人最近新學python ,用到關于機器的相關信息,經過一番研究,從網上查找資料,經過測試,總結了一下相關的方法.
# -*- coding: UTF8 -*-import osimport win32apiimport datetimeimport platformimport getpassimport socketimport uuidimport _winregimport 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()))
新聞熱點
疑難解答