【问题标题】:How can I detect using Python the insertion of only USBs and hard drives on Ubuntu/Linux?如何使用 Python 检测在 Ubuntu/Linux 上仅插入 USB 和硬盘驱动器?
【发布时间】:2013-11-16 00:36:21
【问题描述】:

我正在构建一个备份程序,该程序涉及检测何时插入可用于备份的媒体。我已经研究过检测备份媒体的插入,我将在/media/username 目录上使用文件系统监视服务 inotify。

问题是我查看了这个目录,并且有些文件夹不代表任何当前可用的媒体。如何检测当前可用介质(USB、HDD)的列表并注意未来的任何介质?从技术上讲,/media/username 目录中主动可用的 USB/HDD 文件夹的特点是什么?

【问题讨论】:

    标签: python linux ubuntu python-3.x usb


    【解决方案1】:
    1. 在 Linux 中,您可以使用内核中的 uevents 并开始嗅探“ACTION”="add" 请检查以下链接:

      http://lwn.net/Articles/242046/

      和 Netlink 套接字: http://www.kernel.org/doc/man-pages/online/pages/man7/netlink.7.html

    2. 或者为 Python 使用 DBUS/HAL API 绑定 http://ubuntuforums.org/archive/index.php/t-904706.html

    3. 检查 dmesg 消息并准确查看附加的内容以及挂载到的分区

    P.S.:这是一个示例(在 SO 上),说明如何在 Python 中使用 DBUS 绑定:

    How can I listen for 'usb device inserted' events in Linux, in Python?

    更新

    如何检查路径是否已挂载: https://serverfault.com/questions/143084/how-can-i-check-whether-a-volume-is-mounted-where-it-is-supposed-to-be-using-pyt

    【讨论】:

    • 是的,我知道如何检测插入,但我如何知道它是具体是 USB 还是 HDD
    【解决方案2】:

    通过监视/dev/disks/by-label/ 目录,我可以很容易地检测到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 2011-03-24
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 2011-12-31
      • 2013-03-06
      • 1970-01-01
      相关资源
      最近更新 更多