简体   繁体   中英

Detecting USB mass storage device connections using python

How can I monitor connections usb mass storage device connections in python? I found a few options but I am stymied at one point or the other in both these options.

  • First is udev (pyudev) which has an excellent system to monitor device connections. Unfortunately, can't get a filter up because I don't know which subsystem, DEVTYPE etc. must be specified.
  • Second, dbus. But HAL seems to be deprecated. Also ran into some problems regarding .service files and stuff. Question here. I feel it's occuring because HAL is deprecated but if it's something else and there's a work around please let me know!

    Have scourged the world wide web and haven't found the solution to the above roadblocks. Please help!

Take a look at gudev

there is a simple example

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

basically, it look like :

#!/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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM