简体   繁体   English

使用python检测USB大容量存储设备连接

[英]Detecting USB mass storage device connections using python

How can I monitor connections usb mass storage device connections in python? 如何在python中监视USB大容量存储设备的连接? 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. 首先是udev(pyudev),它具有一个出色的系统来监视设备连接。 Unfortunately, can't get a filter up because I don't know which subsystem, DEVTYPE etc. must be specified. 不幸的是,由于我不知道必须指定哪个子系统,DEVTYPE等,所以无法进行过滤。
  • Second, dbus. 第二,dbus。 But HAL seems to be deprecated. 但是HAL似乎已被弃用。 Also ran into some problems regarding .service files and stuff. 还遇到了有关.service文件和内容的一些问题。 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! 我感觉这是因为不推荐使用HAL而发生的,但是如果还有其他问题并且可以解决,请告诉我!

    Have scourged the world wide web and haven't found the solution to the above roadblocks. 搜寻万维网,但找不到上述障碍的解决方案。 Please help! 请帮忙!

Take a look at gudev 看看gudev

there is a simple example 有一个简单的例子

http://ubuntuforums.org/archive/index.php/t-1695571.html 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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