从python重新启动本地计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4629131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
restart local computer from python
提问by Claudiu
Is there any way to make the computer a Python program is running on restart? Generic solution is good, but in particular I'm on windows.
有没有办法让计算机在重新启动时运行 Python 程序?通用解决方案很好,但特别是我在 Windows 上。
采纳答案by Lennart Regebro
There is no generic way of doing this, afaik.
没有通用的方法可以做到这一点,afaik。
For Windows, you need to access the Win32 API. Like so:
对于 Windows,您需要访问 Win32 API。像这样:
import win32api
win32api.InitiateSystemShutdown()
The win32api module is a part of pywin32.
win32api 模块是pywin32的一部分。
For linux/os x, I guess calling the "reboot" command is the easiest.
对于 linux/os x,我想调用“reboot”命令是最简单的。
import os
os.system('reboot now')
Or something like that.
或类似的东西。
(Note to downvoters: os.system()has notbeen deprecated. The text is "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." For simple cases like this, when you aren't interested in retrieving the results, nor in multiprocessing, os.system()works just fine).
(注到downvoters:os.system()还没有被弃用的文本;对于简单的情况就是这样,当你AREN。“该子模块提供更强大的设施产生新的流程和检索的结果使用该模块最好使用此功能。”对检索结果不感兴趣,也不对多处理感兴趣,os.system()工作得很好)。
回答by Malachi Bazar
You could reboot a Windows system by using: os.system("shutdown -t 0 -r -f")
您可以使用以下命令重新启动 Windows 系统: os.system("shutdown -t 0 -r -f")
Example:
例子:
import os
print "REBOOTING"
os.system("shutdown -t 0 -r -f")
Change the number in front of -t to change the number of seconds before shutdown.
更改 -t 前面的数字以更改关闭前的秒数。
回答by Simon
There is nothing in the standard library that would directly allow you to do this, and I am unaware of any modules that provide a cross platform method, but if you are on Windows and don't want to install anything (like the win32api) then you could use ctypesdefault module and interact with the WinAPI directly.
标准库中没有任何内容可以直接允许您执行此操作,而且我不知道任何提供跨平台方法的模块,但是如果您使用的是 Windows 并且不想安装任何东西(例如 win32api),那么您可以使用ctypes默认模块并直接与 WinAPI 交互。
You could use the ExitWindowsEx()function to restart the computer (it is almost the same as my other answer How to shudown a computer using Python; however the hexadecimal value has to be changed to restart and not shutdown).
您可以使用该ExitWindowsEx()函数重新启动计算机(它与我的其他答案How to shudown a computer using Python几乎相同;但是必须将十六进制值更改为重新启动而不是关闭)。
Process:
过程:
First you need ctypes:
首先你需要ctypes:
import ctypes
Next get the user32.dllas described in the documentation:
接下来user32.dll按照文档中的描述获取:
DLL | User32.dll
DLL | 用户32.dll
So:
所以:
user32 = ctypes.WinDLL('user32')
Next you need to call the ExitWindowsEx()function and insert the correct hexadecimal values:
接下来您需要调用该ExitWindowsEx()函数并插入正确的十六进制值:
user32.ExitWindowsEx(0x00000002, 0x00000000)
The first argument (0x00000002) shuts down the system and then restarts (see documentation).
第一个参数 ( 0x00000002) 关闭系统,然后重新启动(请参阅文档)。
The second argument (0x00000000) gives a reason to be logged by the system. A complete list can be found here
第二个参数 ( 0x00000000) 给出了被系统记录的原因。完整列表可以在这里找到
Complete Code:
完整代码:
import ctypes
user32 = ctypes.WinDLL('user32')
user32.ExitWindowsEx(0x00000002, 0x00000000)
About os.system()method on Windows:
关于os.system()Windows 上的方法:
The win32api or ctypes answers both will execute silently. os.system("shutdown -t 0 -r -f")will leave a message saying "You are about to be signed out in less than a minute" which may be undesirable in some cases.
win32api 或 ctypes 答案都将静默执行。 os.system("shutdown -t 0 -r -f")将留下一条消息,说“您将在不到一分钟的时间内退出”,这在某些情况下可能是不可取的。
A file alongside the script called shutdown.bat/shutdown.exe/shutdown.cmdwill cause the command shutdown -t 0 -r -fto break, calling that file and not the system command. The same goes for described wmic.exeabove.
旁边的脚本的文件名为shutdown.bat/ shutdown.exe/shutdown.cmd将导致命令shutdown -t 0 -r -f来休息,调用该文件,而不是系统命令。wmic.exe上面描述的也是如此。
As a side note:I built WinUtils(Windows only) which simplifies this a bit, however it should be faster (and does not require Ctypes) since it is built in C.
附带说明:我构建了WinUtils(仅限 Windows),它稍微简化了这一点,但是它应该更快(并且不需要 Ctypes),因为它是用 C 构建的。
Example:
例子:
import WinUtils
WinUtils.Restart(WinUtils.SHTDN_REASON_MINOR_OTHER)

