使用Python检测USB大容量存储设备连接
我想知道怎么用Python监控USB大容量存储设备的连接。我找到了一些方法,但在这两种方法中都遇到了一些问题。
第一种是使用udev(pyudev),这个系统可以很好地监控设备连接。不过,我不知道该怎么设置过滤器,因为我不清楚需要指定哪个子系统、DEVTYPE等。
第二种是dbus。但是HAL似乎已经不再使用了。我还遇到了一些关于.service文件的问题。这里有个问题。我觉得问题可能是因为HAL不再使用了,但如果是其他原因,并且有解决办法的话,请告诉我!
我在网上找了很多资料,但还是没有找到解决这些问题的方法。请帮帮我!
1 个回答
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()