Python USB检测 [英] Python usb detection

查看:668
本文介绍了Python USB检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


首先抱歉我的英语!

我的环境:
python:2.7.3
wxwidgets:2.9.4-1
wxpython:2.9.4-1
ubuntu:12.04

上下文:
我必须检测USB硬盘驱动器何时插入或拔出,​​并对其执行一些操作.
例如,当插入磁盘时,我希望获得安装点(例如:/media/usb0)和系统点(例如:/dev/sdb1).我需要两条路径,而且我不想进行系统调用,例如(subprocess:mount -l).

我尝试了几种方法:
-pyudev:仅获取EVT_DEVICE_ADDED上的系统路径(如/dev/sdb1)
-Gio(gi.repository):使用"mount-added"(如/media/usb0)获取安装点,并在第二个事件"volume-added"中获取系统点,但是我在Gio中遇到问题,添加和删除事件失败或怀疑行为取决于计算机,我已经在计算机上尝试过我的应用程序
-DBusGMainLoop(dbus.mainloop.glib):可以运行,但取决于我尝试过的计算机(全部使用相同的配置)启动2事件"DeviceAdded",有时启动一个DeviceChanged,但有时在插入磁盘时不启动.

您是否知道检测USB磁盘何时插入的方法(也许是我暴露的3个问题中的一个,我做得不好),请调用一种方法,并在此方法中获取我需要的2条路径?

预先感谢.

欧瑞莲.

解决方案

我用它来检查连接的USB设备:

要求

  • pyusb

示例

import usb
from usb.core import USBError

### Some auxiliary functions ###
def _clean_str(s):
    '''
    Filter string to allow only alphanumeric chars and spaces

    @param s: string
    @return: string
    '''

    return ''.join([c for c in s if c.isalnum() or c in {' '}])


def _get_dev_string_info(device):
    '''
    Human readable device's info

    @return: string
    '''

    try:
        str_info = _clean_str(usb.util.get_string(device, 256, 2))
        str_info += ' ' + _clean_str(usb.util.get_string(device, 256, 3))
        return str_info
    except USBError:
        return str_info


def get_usb_devices():
    '''
    Get USB devices

    @return: list of tuples (dev_idVendor, dev_idProduct, dev_name)
    '''

    return [(device.idVendor, device.idProduct, _get_dev_string_info(device)) 
                for device in usb.core.find(find_all=True)
                    if device.idProduct > 2]

希望对您有所帮助!我在此处

有更多与USB相关的代码


First sorry for my english !

my environement :
python : 2.7.3
wxwidgets : 2.9.4-1
wxpython : 2.9.4-1
ubuntu : 12.04

context :
I have to detect when an usb hard-drive is plugged or unplugged and do some action on it.
For example when a disk is plugged i wan to get the mount point (ex:/media/usb0) and the system point (ex:/dev/sdb1). I need both two path and i do not want made a system call like (subprocess : mount -l).

I have tried several ways :
- pyudev : only get the system path on EVT_DEVICE_ADDED (like /dev/sdb1)
- Gio (gi.repository) : get the mount point with 'mount-added' (like /media/usb0) and the system point in an second event 'volume-added' but i have problems with Gio add and remove event fail or have suspect behavior depends on computer i have tried my application on it
- DBusGMainLoop (dbus.mainloop.glib) : Works but depends on computer i tried it (all on the same configuration) launch 2 event 'DeviceAdded', and sometime one DeviceChanged but sometime not when a disk is plugged.

Do you know a way (maybe one of the 3 i exposed, i have done something bad) to detect when an usb disk is plugged, call a method and in this method get the 2 path i need ?

Thanks in advance.

Aurélien.

解决方案

I use this to check attached USB devices:

Requirements

  • pyusb

Example

import usb
from usb.core import USBError

### Some auxiliary functions ###
def _clean_str(s):
    '''
    Filter string to allow only alphanumeric chars and spaces

    @param s: string
    @return: string
    '''

    return ''.join([c for c in s if c.isalnum() or c in {' '}])


def _get_dev_string_info(device):
    '''
    Human readable device's info

    @return: string
    '''

    try:
        str_info = _clean_str(usb.util.get_string(device, 256, 2))
        str_info += ' ' + _clean_str(usb.util.get_string(device, 256, 3))
        return str_info
    except USBError:
        return str_info


def get_usb_devices():
    '''
    Get USB devices

    @return: list of tuples (dev_idVendor, dev_idProduct, dev_name)
    '''

    return [(device.idVendor, device.idProduct, _get_dev_string_info(device)) 
                for device in usb.core.find(find_all=True)
                    if device.idProduct > 2]

I hope it helps! I have some more code related to USB stuff here

这篇关于Python USB检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆