Skip to main content

Windows USB 热拔插监控库

Project description

cyme_win

Windows USB hotplug monitor based on cyme, simple and easy to use.

Installation

pip install cyme_win

Quick Start

import cyme_win

# Download cyme.exe (first time only)
cyme_win.download_cyme()

# Simple callback - one line to monitor USB hotplug
cyme_win.watch(lambda d: print(f"{d['event']}: {d['name']} ({d['vid_pid']})"))

import time
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    pass

Device Info

The callback receives a dict with these fields:

Field Description Example
event Event type "connect" / "disconnect"
name Device name "SanDisk USB Flash Drive"
vendor_id Vendor ID "0781"
product_id Product ID "5567"
vid_pid Combined format "0781:5567"
location Linux-style location "0-15"
manufacturer Manufacturer name "SanDisk"
serial Device serial number "4C5300012345"
speed Transfer speed "480.0 Mb/s"
device_class USB device class "mass-storage"
bus Bus number 0
port Port number 15

Examples

Example 1: Distinguish connect/disconnect events

import cyme_win

def on_device(d):
    symbol = "+" if d["event"] == "connect" else "-"
    print(f"[{symbol}] {d['name']}")
    print(f"    VID/PID: {d['vid_pid']}")
    print(f"    Location: {d['location']}")

cyme_win.watch(on_device)

import time
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("Stopped")

Example 2: Monitor specific devices

import cyme_win

def on_storage(d):
    # Only monitor mass storage devices
    if d.get("device_class") == "mass-storage":
        print(f"USB Drive {d['event']}: {d['name']}")
        print(f"  Serial: {d['serial']}")
        print(f"  Speed: {d['speed']}")

cyme_win.watch(on_storage)

Example 3: Using class API (multiple callbacks)

import cyme_win

def log_device(d):
    with open("usb_log.txt", "a") as f:
        f.write(f"{d['event']} | {d['name']} | {d['vid_pid']}\n")

def notify(d):
    print(f"Notify: {d['event']} - {d['name']}")

# Create monitor, add multiple callbacks
monitor = cyme_win.USBMonitor()
monitor.add_callback(log_device)
monitor.add_callback(notify)
monitor.start()

import time
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    monitor.stop()

Example 4: Using context manager

import cyme_win

with cyme_win.USBMonitor().add_callback(lambda d: print(d)):
    print("Monitoring... Press Ctrl+C to exit")
    import time
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass

Example 5: Get full device info

import cyme_win

def show_details(d):
    print(f"\n{'='*50}")
    print(f"Event: {d['event']}")
    print(f"Name: {d['name']}")
    print(f"VID:PID: {d['vid_pid']}")
    print(f"Manufacturer: {d['manufacturer']}")
    print(f"Serial: {d['serial']}")
    print(f"Speed: {d['speed']}")
    print(f"Location: {d['location']} (bus {d['bus']}, port {d['port']})")
    print(f"Class: {d['device_class']}")
    print(f"{'='*50}\n")

cyme_win.watch(show_details)

import time
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    pass

API Reference

download_cyme(target_dir=None)

Download cyme.exe to specified directory (default: current working directory).

cyme_win.download_cyme()  # Current directory
cyme_win.download_cyme("E:/my_project")  # Specified directory

watch(callback)

Global singleton mode, simplest way to use.

cyme_win.watch(lambda d: print(d))

stop()

Stop global monitoring.

cyme_win.stop()

USBMonitor class

Method Description
add_callback(fn) Add event callback, returns self for chaining
start() Start monitoring
stop() Stop monitoring

Links

License

MIT

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

cyme_win-1.5.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

cyme_win-1.5.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file cyme_win-1.5.0.tar.gz.

File metadata

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

File hashes

Hashes for cyme_win-1.5.0.tar.gz
Algorithm Hash digest
SHA256 fb171316374ce8a2a504fc6de46d92b32b64b5242f9c1515a76169bc67cd927c
MD5 37e7527dcc852d104a4c97868d1ef339
BLAKE2b-256 ee2600cc30bba276ef989db02fb5d90f90b8950213cfa0d03d9aaea2e54a6393

See more details on using hashes here.

File details

Details for the file cyme_win-1.5.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for cyme_win-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8abbc67ee3f17b5a43680bb182065c79fe3fbfb6d7228cd7b7453cae3b00ccc5
MD5 07b7859e0ff7d466bf9da79b02eb4c73
BLAKE2b-256 3044c229e642e52ecb1d38d60134ef14ba15509099c91d255476d58ef27e9a16

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