【问题标题】:Sleep / Suspend / Hibernate Windows PC睡眠/暂停/休眠 Windows PC
【发布时间】:2011-11-22 23:13:57
【问题描述】:

我想编写一个简短的 Python 脚本,让我的计算机进入睡眠状态。我已经搜索了 API,但挂起的唯一结果与延迟执行有关。诀窍是什么功能?

【问题讨论】:

  • PC 是否暗示这是在 Windows 上?

标签: python windows sleep suspend


【解决方案1】:

获取pywin32,如果我没记错的话,它还包含win32security。然后再次尝试提到的script

【讨论】:

    【解决方案2】:

    如果您使用的是 Windows,请参阅 Tim Golden 的 gmane.comp.python.windows 新闻组 post

    【讨论】:

    • 我关注了您的链接,但该脚本对我不起作用。如何获得 win32api 和 win32security,这两个导入都不起作用。
    【解决方案3】:
    subprocess.call(['osascript', '-e','tell app "System Events" to sleep'])
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    【解决方案4】:

    如果您有 pywin32 和 ctypes,则无需借助 shell 执行:

    import ctypes
    import win32api
    import win32security
    
    def suspend(hibernate=False):
        """Puts Windows to Suspend/Sleep/Standby or Hibernate.
    
        Parameters
        ----------
        hibernate: bool, default False
            If False (default), system will enter Suspend/Sleep/Standby state.
            If True, system will Hibernate, but only if Hibernate is enabled in the
            system settings. If it's not, system will Sleep.
    
        Example:
        --------
        >>> suspend()
        """
        # Enable the SeShutdown privilege (which must be present in your
        # token in the first place)
        priv_flags = (win32security.TOKEN_ADJUST_PRIVILEGES |
                      win32security.TOKEN_QUERY)
        hToken = win32security.OpenProcessToken(
            win32api.GetCurrentProcess(),
            priv_flags
        )
        priv_id = win32security.LookupPrivilegeValue(
           None,
           win32security.SE_SHUTDOWN_NAME
        )
        old_privs = win32security.AdjustTokenPrivileges(
            hToken,
            0,
            [(priv_id, win32security.SE_PRIVILEGE_ENABLED)]
        )
    
        if (win32api.GetPwrCapabilities()['HiberFilePresent'] == False and
            hibernate == True):
                import warnings
                warnings.warn("Hibernate isn't available. Suspending.")
        try:
            ctypes.windll.powrprof.SetSuspendState(not hibernate, True, False)
        except:
            # True=> Standby; False=> Hibernate
            # https://msdn.microsoft.com/pt-br/library/windows/desktop/aa373206(v=vs.85).aspx
            # says the second parameter has no effect.
    #        ctypes.windll.kernel32.SetSystemPowerState(not hibernate, True)
            win32api.SetSystemPowerState(not hibernate, True)
    
        # Restore previous privileges
        win32security.AdjustTokenPrivileges(
            hToken,
            0,
            old_privs
        )
    

    如果您只想要一个带有 pywin32 的单线并且已经拥有正确的权限(对于 简单,个人脚本):

    import win32api
    win32api.SetSystemPowerState(True, True)  # <- if you want to Suspend
    win32api.SetSystemPowerState(False, True)  # <- if you want to Hibernate
    

    注意:如果您的系统禁用了休眠,它将暂停。在第一个函数中,我包含了一个检查以至少对此发出警告。

    【讨论】:

      【解决方案5】:

      我不知道怎么睡觉。但我知道如何休眠(在 Windows 上)。也许这就足够了? shutdown.exe 是你的朋友!从命令提示符运行它。

      要查看其选项 shutdown.exe /?

      我相信休眠调用将是: shutdown.exe /h

      所以,把它们放在 python 中:

      import os
      os.system("shutdown.exe /h")
      

      但是正如其他人提到的,使用 os.system 是不好的。改用popen。但是,如果你像我一样懒惰,那就是一个小脚本,嗯! os.system 是给我的。

      【讨论】:

        【解决方案6】:
        import os
        os.system(r'rundll32.exe powrprof.dll,SetSuspendState Hibernate')
        

        【讨论】:

          猜你喜欢
          • 2020-09-20
          • 2012-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-10
          相关资源
          最近更新 更多