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

推荐订阅源

博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
B
Blog
博客园_首页
量子位
博客园 - 叶小钗
L
LangChain Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
D
DataBreaches.Net
雷峰网
雷峰网
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
I
Intezer
H
Help Net Security
The Hacker News
The Hacker News
The Register - Security
The Register - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
Troy Hunt's Blog
L
LINUX DO - 热门话题

博客园 - 飘啊飘

一个精简的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()