import ctypes, atexit # DWORD SetThreadExecutionState(EXECUTION_STATE esFlags); kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) SetThreadExecutionState = kernel32.SetThreadExecutionState SetThreadExecutionState.argtypes = [ctypes.c_uint] SetThreadExecutionState.restype = ctypes.c_uint ES_CONTINUOUS = 0x80000000 ES_DISPLAY_REQUIRED = 0x00000002 # Optional: ES_SYSTEM_REQUIRED = 0x00000001 def keep_awake(): r = SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED) if r == 0: raise ctypes.WinError(ctypes.get_last_error()) def clear_awake(): SetThreadExecutionState(ES_CONTINUOUS) atexit.register(clear_awake) keep_awake() input("Display is kept on. Press Enter to release and exit...")