python调用exe执行权限_如何在windows上运行具有提升权限的python脚本

谢谢大家的回复。早在2010年,我就用普雷斯顿·兰德斯编写的模块/脚本编写了我的脚本。在浏览了两天的互联网之后,我可以找到这个脚本,因为它深深地隐藏在pywin32邮件列表中。使用这个脚本可以更容易地检查用户是否是管理员,如果不是,则请求UAC/admin权限。它确实在单独的窗口中提供输出,以了解代码正在做什么。关于如何使用脚本中包含的代码的示例。为了所有在windows上寻找UAC的人的利益,请看一下下面的代码。我希望它能帮助人们寻找同样的解决方案。它可以用你的主要脚本中的这样的东西:import admin

if not admin.isUserAdmin():

admin.runAsAdmin()

实际代码是:#!/usr/bin/env python

# -*- coding: utf-8; mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-

# vim: fileencoding=utf-8 tabstop=4 expandtab shiftwidth=4

# (C) COPYRIGHT © Preston Landers 2010

# Released under the same license as Python 2.6.5

import sys, os, traceback, types

def isUserAdmin():

if os.name == 'nt':

import ctypes

# WARNING: requires Windows XP SP2 or higher!

try:

return ctypes.windll.shell32.IsUserAnAdmin()

except:

traceback.print_exc()

print "Admin check failed, assuming not an admin."

return False

elif os.name == 'posix':

# Check for root on Posix

return os.getuid() == 0

else:

raise RuntimeError, "Unsupported operating system for this module: %s" % (os.name,)

def runAsAdmin(cmdLine=None, wait=True):

if os.name != 'nt':

raise RuntimeError, "This function is only implemented on Windows."

import win32api, win32con, win32event, win32process

from win32com.shell.shell import ShellExecuteEx

from win32com.shell import shellcon

python_exe = sys.executable

if cmdLine is None:

cmdLine = [python_exe] + sys.argv

elif type(cmdLine) not in (types.TupleType,types.ListType):

raise ValueError, "cmdLine is not a sequence."

cmd = '"%s"' % (cmdLine[0],)

# XXX TODO: isn't there a function or something we can call to massage command line params?

params = " ".join(['"%s"' % (x,) for x in cmdLine[1:]])

cmdDir = ''

showCmd = win32con.SW_SHOWNORMAL

#showCmd = win32con.SW_HIDE

lpVerb = 'runas' # causes UAC elevation prompt.

# print "Running", cmd, params

# ShellExecute() doesn't seem to allow us to fetch the PID or handle

# of the process, so we can't get anything useful from it. Therefore

# the more complex ShellExecuteEx() must be used.

# procHandle = win32api.ShellExecute(0, lpVerb, cmd, params, cmdDir, showCmd)

procInfo = ShellExecuteEx(nShow=showCmd,

fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,

lpVerb=lpVerb,

lpFile=cmd,

lpParameters=params)

if wait:

procHandle = procInfo['hProcess']

obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE)

rc = win32process.GetExitCodeProcess(procHandle)

#print "Process handle %s returned code %s" % (procHandle, rc)

else:

rc = None

return rc

def test():

rc = 0

if not isUserAdmin():

print "You're not an admin.", os.getpid(), "params: ", sys.argv

#rc = runAsAdmin(["c:\\Windows\\notepad.exe"])

rc = runAsAdmin()

else:

print "You are an admin!", os.getpid(), "params: ", sys.argv

rc = 0

x = raw_input('Press Enter to exit.')

return rc

if __name__ == "__main__":

sys.exit(test())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值