python –获取windows(mac地址/主板序列号)

获取m

ac地址

import psutil


def get_mac_address():
    # 获取所有网络接口
    interfaces = psutil.net_if_addrs()

    # 遍历接口并找到MAC地址
    for interface_name, interface_addresses in interfaces.items():
        for address in interface_addresses:
            if address.family == psutil.AF_LINK:
                return address.address

    return None


mac_address = get_mac_address()
if mac_address:
    print("MAC地址:", mac_address)
else:
    print("无法获取MAC地址")

获取主板序列号

import wmi

# 创建WMI对象
c = wmi.WMI()

# 查询主板信息
board_info = c.Win32_BaseBoard()[0]

# 获取主板序列号
serial_number = board_info.SerialNumber

# 打印主板序列号
print("主板序列号:", serial_number)

注意在多线程中试用wmi导致错误

import pythoncom
import wmi
from flask import jsonify
from loguru import logger

class GetZhubanView(object):
    '''获取主板信息'''

    @staticmethod
    def get(request, *args, **kwargs) -> jsonify:
        '''获取主板信息(无参数)'''
        serial_number = GetZhubanView.get_serial_number()
        logger.debug(f'设备主板号为:【{serial_number}】')
        return jsonify({'code': 0, 'msg': 'success', 'serial_number': serial_number})


    @staticmethod
    def get_serial_number():
        '''获取主板序列号'''
        pythoncom.CoInitialize()  # 开启
        c = wmi.WMI()
        board_info = c.Win32_BaseBoard()[0]
        pythoncom.CoUninitialize()  # 释放
        serial_number = board_info.SerialNumber
        return serial_number