使用python检测USB大容量存储设备连接

时间:2014-03-12 02:02:46

标签: python usb

如何在python中监控连接usb大容量存储设备连接?我发现了一些选项,但我在这两个选项中都被阻碍了。

  • 首先是udev(pyudev),它有一个很好的系统来监控设备连接。不幸的是,无法过滤,因为我不知道必须指定哪个子系统,DEVTYPE等。
  • 第二,dbus。但是HAL似乎已被弃用了。还遇到了一些关于.service文件和东西的问题。 Question在这里。我觉得它已经发生了,因为HAL已被弃用,但如果它是别的东西并且有解决方法请告诉我!

    肆虐万维网并且找不到上述障碍的解决方案。请帮忙!

1 个答案:

答案 0 :(得分:0)

看看gudev

有一个简单的例子

http://ubuntuforums.org/archive/index.php/t-1695571.html

基本上,它看起来像:

#!/usr/bin/env python

import glib
import gudev
import pynotify
import sys


def callback(client, action, device, user_data):
device_vendor = device.get_property("ID_VENDOR_ENC")
device_model = device.get_property("ID_MODEL_ENC")
if action == "add":
    n = pynotify.Notification("USB Device Added", "%s %s is now connected to your system" % (device_vendor,device_model))
    n.show()
elif action == "remove":
    n = pynotify.Notification("USB Device Removed", "%s %s has been disconnected from your system" %
(device_vendor, device_model))
n.show()


if not pynotify.init("USB Device Notifier"):
    sys.exit("Couldn't connect to the notification daemon!")

client = gudev.Client(["usb/usb_device"])
client.connect("uevent", callback, None)

loop = glib.MainLoop()
loop.run()
相关问题