Skip to main content

A high-performance Windows USB device monitoring tool

Project description

USB设备监控工具 v1.9

一个高性能的Windows USB设备监控工具,用于实时检测USB设备的插入和移除。

功能特点

  • 实时监测USB设备的插拔事件
  • 获取USB设备的详细信息,包括设备ID、制造商、设备名称等
  • 支持获取设备的实际ID和物理位置信息
  • 支持自定义事件回调函数
  • 高性能设计,占用系统资源少
  • 标准日志输出,方便集成到其他系统
  • 可排除特定类型的设备服务(如磁盘等)

安装方法

pip install usbjiance

快速开始

基本用法示例:

from usbjiance import USBEventMonitor
import time

# 定义回调函数
def on_connect(info):
    print(f"设备已连接: {info.get('设备ID')}")
    
def on_disconnect(info):
    print(f"设备已断开: {info.get('设备ID')}")

# 创建监控器实例
monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    enable_print=True  # 启用日志打印
)

# 在主线程中启动监控
try:
    monitor.start()
except KeyboardInterrupt:
    # 处理Ctrl+C中断
    pass
finally:
    # 确保正确停止监控
    monitor.stop()

配置选项

USBEventMonitor类接受以下参数:

参数 类型 默认值 说明
on_connect Callable None USB设备连接时的回调函数
on_disconnect Callable None USB设备断开时的回调函数
check_interval float 0.2 检查间隔时间(秒)
enable_print bool False 是否打印设备信息到日志
enable_real_id bool True 是否获取实际设备ID
enable_location bool True 是否获取设备位置信息
exclude_services List[str] ['disk', 'USBSTOR'] 要排除的服务列表

回调函数

回调函数接收一个包含设备信息的字典参数:

def on_connect(info: Dict[str, Any]) -> None:
    """
    当USB设备连接时调用
    
    参数:
        info: 包含设备信息的字典,至少包含'设备ID'键
    """
    device_id = info.get('设备ID')
    # 处理设备连接事件

设备信息字典包含以下常见字段:

  • 设备ID: 原始设备ID
  • 实际设备ID: 从注册表获取的实际设备ID(如果enable_real_id=True)
  • 设备位置: 设备的物理位置信息(如果enable_location=True)
  • 状态: 设备当前状态,"已连接"或"已断开"
  • 添加时间/断开时间: 事件发生的时间
  • Service: 设备服务名称,可用于识别设备类型
  • 其他设备属性: 如制造商、设备名称等(当可用时)

高级用法

以不同配置运行

最高性能模式(不获取额外信息):

monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    enable_real_id=False,
    enable_location=False
)

获取完整信息模式:

monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    enable_print=True,
    enable_real_id=True,
    enable_location=True
)

自定义排除服务:

# 排除磁盘、存储设备和打印机
monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    exclude_services=['disk', 'USBSTOR', 'usbprint']
)

# 排除特定MTP设备
monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    exclude_services=['disk', 'USBSTOR', 'WUDFWpdMtp']
)

# 不排除任何服务
monitor = USBEventMonitor(
    on_connect=on_connect,
    on_disconnect=on_disconnect,
    exclude_services=[]
)

在线程中运行

import threading

def monitor_thread():
    monitor = USBEventMonitor(
        on_connect=on_connect,
        on_disconnect=on_disconnect
    )
    monitor.start()  # 阻塞调用

# 创建并启动线程
thread = threading.Thread(target=monitor_thread)
thread.daemon = True  # 设置为守护线程
thread.start()

# 主程序继续执行其他任务
# ...

API文档

USBEventMonitor类

class USBEventMonitor:
    def __init__(self,
                 on_connect=None,
                 on_disconnect=None,
                 check_interval=0.2,
                 enable_print=False,
                 enable_real_id=True,
                 enable_location=True,
                 exclude_services=None):
        """初始化USB监控器"""
        # ...
        
    def start(self):
        """启动监控(阻塞方法)"""
        # ...
        
    def stop(self):
        """停止监控"""
        # ...

版本历史

v1.07

  • 使用Python标准库logging模块替代自定义logger
  • 正则表达式模式预编译优化
  • 改进异常处理,支持优雅退出
  • 默认启用设备位置信息获取
  • 添加exclude_services参数,支持排除特定服务类型的设备
  • 将WUDFWpdMtp从默认排除服务列表中移除
  • 其他性能和稳定性改进

v1.06

  • 添加设备位置信息获取功能
  • 性能优化

v1.05

  • 支持获取实际设备ID
  • 增强错误处理

v1.04

  • 支持Windows注册表查询
  • 添加事件回调机制

v1.03

  • 初始功能实现

系统要求

  • Windows 7/8/10/11
  • Python 3.8+
  • 依赖库: wmi, pythoncom

许可证

MIT许可证

贡献

欢迎提交问题报告和功能请求。如果您想贡献代码,请先开issue讨论您想要改变的内容。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

usbjiance-1.9.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

usbjiance-1.9-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file usbjiance-1.9.tar.gz.

File metadata

  • Download URL: usbjiance-1.9.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for usbjiance-1.9.tar.gz
Algorithm Hash digest
SHA256 a6f035ad20e907106aa6c4a393e2d67ae4599a0338f07b383b00a7bd3ac47617
MD5 6d74761f8defa9d5210ef0db6822b118
BLAKE2b-256 af41371aabe1043480309023b6124c7fff5e6cdf119b25d4d47278766767dc58

See more details on using hashes here.

File details

Details for the file usbjiance-1.9-py3-none-any.whl.

File metadata

  • Download URL: usbjiance-1.9-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for usbjiance-1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 916a10f4ef8a54c243624b6c4ce47ffae84c3f1976c92f3bdf2743c9af6fd419
MD5 c0e63cb09568706d01a7277af0e86396
BLAKE2b-256 3749fed9df8f205bc6cf3f8e5b1fe21e1c6e76ccb6604dc8aad9902ff89af5b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page