USB设备UDev和D-BUS

我正在尝试获取Ubuntu 10.10中当前插入USB设备的列表,并监视发生的更改,如使用UDev和D-BUS插入或插入设备。 我是使用D-BUS进行编程的新手。 我看到一个例子: Linux:如何检测是USB键盘插入和拔出只有一个使用HAL,我知道HAL已弃用。 我发现一些工作代码,修改了一下,只有它不适用于任何设备的唯一存储设备,如USB棒,媒体播放器或光盘设备。 我想整个事情鼠标,键盘,USB相机充电器插入USB的任何东西我希望我的程序知道它。 这基本上是我的( http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html ):

import dbus import gobject from dbus.mainloop.glib import DBusGMainLoop def device_added_callback(device): print 'Device %s was added' % (device) def device_changed_callback(device): print 'Device %s was changed' % (device) #must be done before connecting to DBus DBusGMainLoop(set_as_default=True) bus = dbus.SystemBus() proxy = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks") iface = dbus.Interface(proxy, "org.freedesktop.UDisks.Device") devices = iface.get_dbus_method('EnumerateDevices')() print '%s' % (devices) #addes two signal listeners iface.connect_to_signal('DeviceAdded', device_added_callback) iface.connect_to_signal('DeviceChanged', device_changed_callback) #start the main loop mainloop = gobject.MainLoop() mainloop.run() 

任何帮助将是apreciated。 预先感谢Calota Romeo

Solutions Collecting From Web of "USB设备UDev和D-BUS"

显然, udisk D-Bus服务只报告磁盘。

只需直接监控udev(通过libudev,通过pyudev )。

 import pyudev context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) observer = pyudev.pygtk.GUDevMonitorObserver(monitor) observer.connect('device-added', device_added_callback) observer.connect('device-changed', device_changed_callback) monitor.enable_receiving() mainloop = gobject.MainLoop() mainloop.run() 

这是我用来列出已经插入的闪存棒。 您可以根据需要修改脚本

 import dbus bus = dbus.SystemBus() proxy = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager") iface = dbus.Interface(proxy, "org.freedesktop.Hal.Manager") devices = iface.GetAllDevices () for device in devices: try: proxy1 = bus.get_object("org.freedesktop.Hal", device) iface1 = dbus.Interface(proxy1, "org.freedesktop.Hal.Device") props = iface1.GetAllProperties() removable = iface1.GetProperty("storage.removable") usb = iface1.GetProperty("storage.bus") if usb== "usb": print "\n".join(("%s: %s" % (k, props[k]) for k in props)) # shows available properties except: pass 

这就是我用来看看是否有任何插入的USB:

 #!/usr/bin/python import dbus import gobject class DeviceAddedlistner: def __init__(self): self.bus = dbus.SystemBus() self.hal_manager_obj = self.bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager") self.hal_manager = dbus.Interface(self.hal_manager_obj,"org.freedesktop.Hal.Manager") self.hal_manager.connect_to_signal("DeviceAdded", self._filter) def _filter(self, udi): device_obj = self.bus.get_object ("org.freedesktop.Hal", udi) device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device") self.do_something(device) def do_something(self, device): try: usb = device.GetProperty("storage.bus") info = device.GetProperty("info.product") removable = device.GetProperty("storage.removable") print info except: pass#blah blah if __name__ == '__main__': from dbus.mainloop.glib import DBusGMainLoop DBusGMainLoop(set_as_default=True) loop = gobject.MainLoop() DeviceAddedlistner() loop.run() 

以下是UDisks的示例: python udisks – 枚举设备信息