linux系统基于pyudev的USB设备插拔检测

import pyudev

# 创建Context对象
context = pyudev.Context()

# 创建Monitor对象,监控事件
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by('block',device_type='partition')
# 开始监控
monitor.start()

# 遍历检测到的USB设备
for device in iter(monitor.poll, None):
    if device.action == 'add':
        # 打印设备信息
        print("USB设备已添加:")
        print(f"设备路径: {device.device_path}")
        print(f"设备节点: {device.device_node}")
        print(f"设备ID: {device.sys_name}")
        print(f"设备序列号: {device.get('ID_SERIAL_SHORT')}")
        print(f"设备制造商: {device.get('ID_VENDOR_ID')}")
        print(f"设备产品: {device.get('ID_MODEL_ID')}")
        print(f"设备描述: {device.get('ID_MODEL_ENC')}")

    elif device.action == 'remove':
        # 打印设备移除信息
        print("USB设备已移除:")
        print(f"设备路径: {device.device_path}")
        print(f"设备节点: {device.device_node}")
        print(f"设备ID: {device.sys_name}")
        print(f"设备序列号: {device.get('ID_SERIAL_SHORT')}")
        print(f"设备制造商: {device.get('ID_VENDOR_ID')}")
        print(f"设备产品: {device.get('ID_MODEL_ID')}")
        print(f"设备描述: {device.get('ID_MODEL_ENC')}")

你可能感兴趣的:(python,开发语言)