简体   繁体   English

以管理员身份运行 python 应用程序,而不提示进入管理页面

[英]Run python app as admin without prompting for admin page

Can we run python app as admin by storing the admin credentials in python code and when python app needs admin rights it should use the admin credentials stored in it's code and run with admin rights.我们可以通过将管理员凭据存储在 python 代码中来以管理员身份运行 python 应用程序,并且当 python 应用程序需要管理员权限时,它应该使用存储在其代码中的管理员凭据并以管理员权限运行。 But it should not prompt for admin page.但它不应该提示输入管理页面。

Let the admin credentials be Username: admin pass: admin@123让管理员凭据为用户名:admin pass:admin@123

This is the code I successfully used on WinXP and Win7 and I see no reason why it should not work on Win10.这是我在 WinXP 和 Win7 上成功使用的代码,我看不出它为什么不能在 Win10 上运行。

global userH
userH = None
def try_login(name, domain, password):
    global userH
    try:
        logging.info("Trying to login as: %s/%s" % (domain, name))
        userH = win32security.LogonUser(name, domain, password,
                win32security.LOGON32_LOGON_INTERACTIVE,
                win32security.LOGON32_PROVIDER_DEFAULT)
        if None!=userH:
            win32security.ImpersonateLoggedOnUser(userH)
            logging.info("Logged in: %s/%s" % (domain, name))
            return True
    except:
        logging.exception("Windows login")
        if userH:
            try:
                win32api.CloseHandle(userH)
            except:
                pass
        userH = None
    return False

def logout():
    if userH:
        try:
            win32security.RevertToSelf()
            win32api.CloseHandle(userH)
        except:
            pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM