如何检测在python上连接了新的USB设备 [英] How to detect a new usb device is connected on python

查看:50
本文介绍了如何检测在python上连接了新的USB设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些在后台运行的东西,只有在计算机检测到新设备连接后,其余的代码才会运行,有没有什么优雅的方法来做这样的事情?

解决方案

这取决于操作系统

在 linux 中你可以使用 pyudev :

<块引用>

几乎公开了完整的 libudev 功能.您可以:

  • 枚举设备,按特定条件过滤 (pyudev.Context)
  • 查询设备信息、属性和属性,
  • 监控设备,与后台线程同步和异步,或在 Qt 的事件循环内(pyudev.pyqt4,pyudev.pyside)、glib (pyudev.glib) 和 wxPython (pyudev.wx).

https://pyudev.readthedocs.io/en/latest/

源代码在http://pyudev.readthedocs.io/en/v0.14/api/monitor.html,查看receive_device()函数

在 Windows 中,您可以使用 WMI(Windows 管理规范),例如 https://blogs.msdn.microsoft.com/powershell/2007/02/24/displaying-usb-devices-using-wmi/ ( Python 读取设备管理器信息 ) 或像 https://pypi.python.org/pypi/infi.devicemanager

I want to make something which will run on the background and only after the computer detect new device is connected the rest of the code will run, is there any elegant way to do such a thing?

解决方案

this is operating system dependent

in linux you can use pyudev for this :

Almost the complete libudev functionality is exposed. You can:

  • Enumerate devices, filtered by specific criteria (pyudev.Context)
  • Query device information, properties and attributes,
  • Monitor devices, both synchronously and asynchronously with background threads, or within the event loops of Qt (pyudev.pyqt4, pyudev.pyside), glib (pyudev.glib) and wxPython (pyudev.wx).

https://pyudev.readthedocs.io/en/latest/

source code is in http://pyudev.readthedocs.io/en/v0.14/api/monitor.html, see the receive_device() function

in windows you can use the WMI ( Windows Management Instrumentation ) like in https://blogs.msdn.microsoft.com/powershell/2007/02/24/displaying-usb-devices-using-wmi/ ( Python Read the Device Manager Information ) or a python binding like in https://pypi.python.org/pypi/infi.devicemanager

这篇关于如何检测在python上连接了新的USB设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆