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, cyme_path=None)

Global singleton mode, simplest way to use.

# Auto-find cyme.exe
cyme_win.watch(lambda d: print(d))

# Specify cyme.exe path
cyme_win.watch(lambda d: print(d), cyme_path="E:/tools/cyme.exe")

stop()

Stop global monitoring.

cyme_win.stop()

USBMonitor class

Method Description
__init__(cyme_path=None) Create monitor, optionally specify cyme.exe path
add_callback(fn) Add event callback, returns self for chaining
start() Start monitoring
stop() Stop monitoring
# Auto-find cyme.exe
monitor = cyme_win.USBMonitor()

# Specify cyme.exe path
monitor = cyme_win.USBMonitor(cyme_path="E:/tools/cyme.exe")

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.1.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.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cyme_win-1.5.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e90ba0a8e1f6a6d87fe1899d8018014d2427b6363f8e4250f5a9fa0af7f28350
MD5 dfbd72b1681f47d8d3d59ab68f7feaf6
BLAKE2b-256 b456321eec45d6aa83580d4b6e61c72a2df22e88d01b21810845e3fd45d378ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cyme_win-1.5.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 436b0622f5e0c360e72f1701f25dd50815a021737c07c0b5effb0f7018c9e6ab
MD5 9be020bf0f5214157b87bf6241e62910
BLAKE2b-256 8da108866aaa78572aa9dfcaebe5a68603175c34823b8fd53f1a1970618adad5

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