我想写一个简短的python脚本让我的电脑进入睡眠状态。我已经搜索了API,但暂停的唯一结果与延迟执行有关。这个技巧有什么功能?使用python挂起/休眠pc
2
A
回答
0
您可以从python脚本运行shell命令。请参阅subprocess module,然后为您的操作系统搜索适当的命令。
1
如果你使用的是Windows,请参阅:http://permalink.gmane.org/gmane.comp.python.windows/7382
+1
我跟着你的链接,但脚本不适合我。我怎样才能获得win32api和win32security,这两种导入都不起作用。 – lhk
2
1
import os
os.system(r'rundll32.exe powrprof.dll,SetSuspendState Hibernate')
4
我不知道该如何睡觉。但我知道如何休眠(在Windows上)。也许这就够了? shutdown.exe
是你的朋友!从命令提示符运行它。
以查看其选项做 shutdown.exe /?
相信休眠电话是: shutdown.exe /h
所以,把他们放在一起在python:
import os
os.system("shutdown.exe /h")
但在其他已经提到,这是坏使用os.system。改用popen。但是,如果你像我一样懒惰,而且有点脚本,他们就是我! os.system它适合我。
2
没有求助于shell执行,如果你有pywin32和ctypes的:
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
注:如果你的系统禁用休眠状态,将暂停。在第一个函数中,我至少包含了一个支票,以提醒您。
相关问题
- 1. 休眠弹簧挂起
- 2. 休眠 - 连接挂起
- 3. 休眠挂起上saveOrUpdate
- 4. 休眠挂在tx.commit
- 5. 休眠:@SecondaryTable不起作用
- 6. 休眠prepareStatement不起作用
- 7. 休眠:@GeneratedValue不起作用?
- 8. Python:打印不起作用,脚本无休止地挂起
- 9. 如何使用SetSuspendState()api进行休眠或在win32 sdk中挂起?
- 10. Sql命令无法完成 - 在控制台中挂起 - 休眠
- 11. 如何检测系统电源事件,如挂起,休眠等
- 12. 使用DAO休眠
- 13. 挂毯休眠和2个数据库
- 14. 休眠@DiscriminatorValue不拿起
- 15. 使用python关闭,重启,休眠通知在豹使用python
- 16. python popen.stdout.readline()挂起
- 17. python thread.join()挂起
- 18. 休眠:使
- 19. 在C#发送键 “挂起” PC
- 20. 暂时挂起PC操作系统
- 21. 休眠5.2.9:@Basic(fetch = FetchType.LAZY)不起作用
- 22. 休眠当前日期不起作用
- 23. 休眠级联操作不起作用
- 24. 休眠级联删除不起作用
- 25. 休眠@Formula注释不起作用
- 26. Log4j2/JPA /休眠日志不起作用
- 27. 休眠更新实体不起作用
- 28. 休眠删除不起作用
- 29. Java休眠:@OneToMany协会不起作用
- 30. 休眠ddl-auto =更新不起作用
PC意味着这是在Windows上吗? – arunkumar