惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
U
Unit 42
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
L
LangChain Blog
Engineering at Meta
Engineering at Meta
量子位
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
A
About on SuperTechFans
Y
Y Combinator Blog

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] 在linux中通过进程名获得进程id busybox0.60.3源码学习开始 pygame做的贪吃蛇 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 哲学家吃空心粉问题 《代码整洁之道》笔记之函数 使用面向对象概念优化条件判断语句的一个小应用 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 解决LINUX和WINDOWS时间不一置 通过状态机实现的一个配置读取函数 UNIX基础知识--《APUE》第一章笔记
pywin32重启电脑 - 飘啊飘 - 博客园
飘啊飘 · 2011-01-29 · via 博客园 - 飘啊飘

最近一直忙着乱七八糟的事,还做了几天义工,没怎么写程序
今天打算用PYTHON来做个WIN32下的定时重启功能来限制弟弟玩游戏
看了下PYTHON原生的,可以通过命令行来调用shutdown命令,但PYWIN32这个库可以让用户直接调用WIN32 API,给力。
于是看看API吧

找到了这个API:

InitiateSystemShutdown

MSDN中已经给出了详细的说明 ,我就不多说了
MSDN中也给出个例子,需要注意的是,需要在使用这个函数前要提升进程的权限,否则会出这个问题:

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
error: (5, 'InitiateSystemShutdown', '\xbe\xdc\xbe\xf8\xb7\xc3\xce\xca\xa1\xa3')

最开始我也没注意到,后来在个英文网页找到了答案:

http://book.opensourceproject.org.cn/lamp/python/pythonwin/opensource/pythonwin32_snode127.html

里面也有pywin32实现reboot的源码,可以拿来直接用(测试环境:XP SP3)
怕有些朋友不方便,我把代码贴出吧:

代码

#!/usr/bin/env python
#
 -*- coding: utf-8 -*-
import win32security
import win32api
import sys
import time
from ntsecuritycon import *def AdjustPrivilege(priv, enable = 1):
    
# Get the process token.
    flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
    htoken 
= win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
    
# Get the ID for the system shutdown privilege.
    id = win32security.LookupPrivilegeValue(None, priv)
    
# Now obtain the privilege for this process.
    # Create a list of the privileges to be added.
    if enable:
        newPrivileges 
= [(id, SE_PRIVILEGE_ENABLED)]
    
else:
        newPrivileges 
= [(id, 0)]
    
# and make the adjustment.
    win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)def RebootServer(message="Server Rebooting", timeout=30, bForce=0, bReboot=1):
    AdjustPrivilege(SE_SHUTDOWN_NAME)
    
try:
        win32api.InitiateSystemShutdown(None, message, timeout, bForce, bReboot)
    
finally:
        
# Now we remove the privilege we just added.
        AdjustPrivilege(SE_SHUTDOWN_NAME, 0)def AbortReboot():
    AdjustPrivilege(SE_SHUTDOWN_NAME)
    
try:
        win32api.AbortSystemShutdown(None)
    
finally:
        
# Now we remove the privilege we just added.
        AdjustPrivilege(SE_SHUTDOWN_NAME, 0)
            
if __name__=='__main__':
        message 
= "This server is pretending to reboot\r\n"
        message 
= message + "The shutdown will stop in 10 seconds"
        RebootServer(message)
        
print "Sleeping for 10 seconds"
        time.sleep(
10)
        
print "Aborting shutdown"
        AbortReboot()