# import import pyudev #pyudev version print pyudev.__version__ #udev version # http://lwn.net/Articles/490413/ print pyudev.udev_version() # create a context object we will use throughout this notebook context = pyudev.Context() # list all the subsystems device_subsystems=[] for device in context.list_devices(): device_subsystems.append(device.subsystem) print set(device_subsystems) # list devices in the network subsystem for net_dev in context.list_devices(subsystem='net'): print net_dev # access CPU device properties for dev in context.list_devices(subsystem='cpu'): print cpu_dev.device_path attributes = dev.attributes for a in attributes: try: print a, attributes[a] except KeyError: pass # allow a line before the next device print # access the properties of the devices in the graphics subsystem for dev in context.list_devices(subsystem='graphics'): print dev.device_path attributes = dev.attributes for a in attributes: try: print a, attributes[a] except KeyError: pass # allow a line before the next device print # access properties from the USB sub system for dev in context.list_devices(subsystem='usb'): print dev.device_path attributes = dev.attributes for a in attributes: try: print a, attributes[a] except KeyError: pass print # access properties from the sound sub system for dev in context.list_devices(subsystem='usb'): print dev.device_path attributes = dev.attributes for a in attributes: try: print a, attributes[a] except KeyError: pass print #Device monitoring demo #this is blocking #for async monitoring, see http://pyudev.readthedocs.org/en/latest/guide.html#asynchronous-monitoring monitor = pyudev.Monitor.from_netlink(context) # monitoring the USB HID subsystem monitor.filter_by('hid') for device in iter(monitor.poll, None): print device, device.action # i plugged out and then in a USB keyboard # The traceback is due to me manually ending the polling loop # also notice the "add" action after the tracback # create a device entry directly device = pyudev.Device.from_path(context, '/sys/devices/system/cpu/cpu0') for a in device.attributes: print a