我想寫一個簡短的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