Python實(shí)現(xiàn)讀取機(jī)器硬件信息的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)讀取機(jī)器硬件信息的方法。分享給大家供大家參考,具體如下:
本人最近新學(xué)python ,用到關(guān)于機(jī)器的相關(guān)信息,經(jīng)過(guò)一番研究,從網(wǎng)上查找資料,經(jīng)過(guò)測(cè)試,總結(jié)了一下相關(guān)的方法.
# -*- coding: UTF8 -*- import os import win32api import datetime import platform import getpass import socket import uuid import _winreg import re
1、讀取注冊(cè)表獲取操作系統(tǒng)版本名稱
def GetOsName():
'''操作系統(tǒng)名稱'''
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、讀取注冊(cè)表獲取操作系統(tǒng)當(dāng)前版本號(hào)
def GetOsVersion():
'''操作系統(tǒng)版本'''
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、讀取注冊(cè)表獲取操作系統(tǒng)的型號(hào)
def GetOsModel():
'''操作系統(tǒng)型號(hào)'''
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、根據(jù)文件的全路徑獲取文件的版本號(hào)
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、通過(guò)platform模塊讀取機(jī)器的其它信息
def get_architecture():
'''獲取操作系統(tǒng)的位數(shù)'''
return platform.architecture()
def get_machine():
'''計(jì)算機(jī)類型'''
return platform.machine()
def get_node():
'''計(jì)算機(jī)的網(wǎng)絡(luò)名稱'''
return platform.node()
def get_processor():
'''計(jì)算機(jī)處理器信息'''
return platform.processor()
def get_system():
'''獲取操作系統(tǒng)類型'''
return platform.system()
def get_TotalInfo():
'''匯總信息'''
return platform.uname()
def get_localDataPath():
'''當(dāng)前用戶路徑'''
return os.path.expanduser('~')
def get_UserName():
'''當(dāng)前用戶名'''
return getpass.getuser()
def get_ComputerName1():
'''獲取機(jī)器名稱'''
return platform.node()()
def get_ComputerName():
'''獲取機(jī)器名稱'''
return socket.gethostname()
def get_AddressIp():
'''獲取本機(jī)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('操作系統(tǒng)的位數(shù) : [{}]'.format(get_architecture()))
print('計(jì)算機(jī)類型 : [{}]'.format(get_machine()))
print('計(jì)算機(jī)的網(wǎng)絡(luò)名稱 : [{}]'.format(get_node()))
print('計(jì)算機(jī)處理器信息 : [{}]'.format(get_processor()))
print('操作系統(tǒng)類型 : [{}]'.format(get_system()))
print('匯總信息 : [{}]'.format(get_TotalInfo()))
print('當(dāng)前用戶路徑: [{}]'.format(get_localDataPath()))
print('當(dāng)前用戶名: [{}]'.format(get_UserName()))
print('機(jī)器名稱: [{}]'.format(get_ComputerName()))
print('機(jī)器IP: [{}]'.format(get_AddressIp()))
print('MAC地址: [{}]'.format(get_Mac()))
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
web.py在SAE中的Session問(wèn)題解決方法(使用mysql存儲(chǔ))
這篇文章主要介紹了web.py在SAE中的Session問(wèn)題解決方法(使用mysql存儲(chǔ)),本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06
Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn)
本文主要介紹了Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(dlib版)
這篇文章主要介紹了python實(shí)現(xiàn)圖像,視頻人臉識(shí)別(dlib版)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-11-11

