此代码用于检测USB设备的插入,拔出,在Ubuntu下测试通过。
#!/usr/bin/env python
import glib
#apt-get install python-gudev
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":
print 'add'
elif action == "remove":
print 'remove'
client = gudev.Client(["usb/usb_device"])
client.connect("uevent", callback, None)
loop = glib.MainLoop()
loop.run()
代码结束。