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.4.2.tar.gz (7.5 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.4.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cyme_win-1.4.2.tar.gz
  • Upload date:
  • Size: 7.5 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.4.2.tar.gz
Algorithm Hash digest
SHA256 bcc0daa3a2f732faf4842a1a60a62a63222797340839d4048094fdadab74e2fc
MD5 3e3a263d97f10a78499b3e600d93cc64
BLAKE2b-256 db0bc9cf32c953a032971f266ef85e3d68ee874486126916f20fde5729e7ba63

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cyme_win-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 34c8934c4fb09c122ab868a90ff3dd22787e3d73a9f371953dd55071b1aec659
MD5 beb864e8ee4c7c59f44b57b85e78276f
BLAKE2b-256 85a5bf37bb2ab5a30428374b799e5e7594ac414788549f3a8f43dcb67c19a7ff

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