Python实现读取机器硬件信息的方法示例

作者:shaomine 时间:2021-02-04 16:28:55 

本文实例讲述了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程序设计有所帮助。

来源:http://www.cnblogs.com/shaosks/p/5599943.html

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

猜你喜欢

  • Windows系统下mysql5.7.21安装详细教程

    2024-01-21 17:26:38
  • dir()、readdir()、scandir()和glob()四种遍历目录方法及性能分析

    2023-05-25 11:18:27
  • Python基础语言学习笔记总结(精华)

    2023-08-26 21:37:23
  • Python中处理无效数据的详细教程

    2021-11-18 06:11:07
  • J2EE基础应用:J2EE中SQL语句自动构造方法

    2009-09-18 09:06:00
  • 数据库性能优化二:数据库表优化提升性能

    2024-01-22 12:07:19
  • golang方法中receiver为指针与不为指针的区别详析

    2024-04-25 15:06:52
  • Python原始字符串(raw strings)用法实例

    2021-05-04 18:29:27
  • Python使用正则表达式分割字符串的实现方法

    2023-03-12 07:13:49
  • Python获取本机所有网卡ip,掩码和广播地址实例代码

    2021-06-26 20:32:17
  • PyCharm+Miniconda3安装配置教程详解

    2022-03-26 08:31:16
  • python实现FTP文件传输的方法(服务器端和客户端)

    2021-03-29 23:36:08
  • Vue组件间的通信方式详析

    2024-05-09 15:12:29
  • Vuex总体案例详解

    2023-07-02 16:51:52
  • Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解

    2023-11-16 22:45:05
  • ERROR CODE: 1175 YOU ARE USING SAFE UPDATE MODE AN

    2024-01-17 21:57:50
  • 对pandas replace函数的使用方法小结

    2022-07-04 15:20:24
  • jupyter notebook中美观显示矩阵实例

    2023-06-06 18:13:35
  • php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)

    2024-05-22 10:07:10
  • 教你用Python脚本快速为iOS10生成图标和截屏

    2022-03-04 05:08:28
  • asp之家 网络编程 m.aspxhome.com