import fcntl
import os
KDSETLED = 19250
console_fd = os.open('/dev/console', os.O_NOCTTY)
fcntl.ioctl(console_fd, KDSETLED, 4)
fcntl.ioctl(console_fd, KDSETLED, 0)
How to change caps lock status without key press