获取mac地址
import psutil
def get_mac_address():
interfaces = psutil.net_if_addrs()
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
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