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

推荐订阅源

S
Schneier on Security
博客园_首页
量子位
博客园 - 司徒正美
S
SegmentFault 最新的问题
J
Java Code Geeks
小众软件
小众软件
博客园 - 【当耐特】
The Register - Security
The Register - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
Cyberwarzone
Cyberwarzone
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
F
Full Disclosure
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
Check Point Blog
T
Threatpost
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
W
WeLiveSecurity
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
T
Tor Project blog
T
Troy Hunt's Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security Affairs
SecWiki News
SecWiki News

博客园 - 飘啊飘

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