我正在尝试获取Ubuntu 10.10中当前插入的USB设备的列表,并监视发生的更改,比如使用UDev和D-BUS插入或拔出的设备。我对使用D-BUS编程还比较陌生。我看到了一个例子:Linux : How to detect is usb keyboard is plugged and unplugged只有一个使用HAL,我知道HAL被弃用了。我找到了一些工作代码,对它进行了一些修改,但它不适用于任何设备,仅适用于存储设备,如usb记忆棒、媒体播放器或cd-rom设备。我想要所有的东西,鼠标,键盘,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()
任何帮助都是值得的。提前谢谢你, 卡洛塔罗密欧
显然,udisksD-Bus服务只报告磁盘。
直接监视udev(通过libudev,通过pyudev)。
这是我用来列出已经插好的闪存棒。您可以根据需要修改脚本
这就是我用来查看是否有usb插头:
以下是udisk的示例: python udisks - enumerating device information
相关问题 更多 >
编程相关推荐