Simple way to query connected USB devices info in Python? Simple way to query connected USB devices info in Python? python python

Simple way to query connected USB devices info in Python?


I can think of a quick code like this.

Since all USB ports can be accessed via /dev/bus/usb/< bus >/< device >

For the ID generated, even if you unplug the device and reattach it [ could be some other port ]. It will be the same.

import reimport subprocessdevice_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)df = subprocess.check_output("lsusb")devices = []for i in df.split('\n'):    if i:        info = device_re.match(i)        if info:            dinfo = info.groupdict()            dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))            devices.append(dinfo)print devices

Sample output here will be:

[{'device': '/dev/bus/usb/001/009', 'tag': 'Apple, Inc. Optical USB Mouse [Mitsumi]', 'id': '05ac:0304'},{'device': '/dev/bus/usb/001/001', 'tag': 'Linux Foundation 2.0 root hub', 'id': '1d6b:0002'},{'device': '/dev/bus/usb/001/002', 'tag': 'Intel Corp. Integrated Rate Matching Hub', 'id': '8087:0020'},{'device': '/dev/bus/usb/001/004', 'tag': 'Microdia ', 'id': '0c45:641d'}]

Code Updated for Python 3

import reimport subprocessdevice_re = re.compile(b"Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I)df = subprocess.check_output("lsusb")devices = []for i in df.split(b'\n'):    if i:        info = device_re.match(i)        if info:            dinfo = info.groupdict()            dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device'))            devices.append(dinfo)            print(devices)


If you are working on windows, you can use pywin32 (old link: see update below).

I found an example here:

import win32com.clientwmi = win32com.client.GetObject ("winmgmts:")for usb in wmi.InstancesOf ("Win32_USBHub"):    print usb.DeviceID

Update Apr 2020:

'pywin32' release versions from 218 and up can be found here at github. Current version 227.


For a system with legacy usb coming back and libusb-1.0, this approach will work to retrieve the various actual strings. I show the vendor and product as examples. It can cause some I/O, because it actually reads the info from the device (at least the first time, anyway.) Some devices don't provide this information, so the presumption that they do will throw an exception in that case; that's ok, so we pass.

import usb.coreimport usb.backend.libusb1busses = usb.busses()for bus in busses:    devices = bus.devices    for dev in devices:        if dev != None:            try:                xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)                if xdev._manufacturer is None:                    xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)                if xdev._product is None:                    xdev._product = usb.util.get_string(xdev, xdev.iProduct)                stx = '%6d %6d: '+str(xdev._manufacturer).strip()+' = '+str(xdev._product).strip()                print stx % (dev.idVendor,dev.idProduct)            except:                pass