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

yipeiwu_com6年前Python基础

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

相关文章

Python Celery多队列配置代码实例

这篇文章主要介绍了Python Celery多队列配置代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Celery官方文档 项...

Python中optparse模块使用浅析

Python中optparse模块使用浅析

最近遇到一个问题,是指定参数来运行某个特定的进程,这很类似Linux中一些命令的参数了,比如ls -a,为什么加上-a选项会响应。optparse模块实现的也是类似的功能,它是为脚本传递...

Python可变参数函数用法实例

本文实例讲述了Python可变参数函数用法。分享给大家供大家参考。具体如下: #!/usr/bin/python def f1(a,b): print a,b def f2(a,*b...

python实现动态创建类的方法分析

本文实例讲述了python实现动态创建类的方法。分享给大家供大家参考,具体如下: python作为动态语言,如何在运行时动态创建类呢(python Creating classes dy...

Python 2/3下处理cjk编码的zip文件的方法

今天项目中遇到了中文编码的zip文件,处理了蛮长时间,所以记录下,以免下次踩坑。 Python2下 Python2中读取zip文件,zipfile.ZipInfo的filename类型是...