在 Windows 下检查 Python 脚本中的管理员权限的跨平台方法?

发布于 2024-07-24 23:09:44 字数 97 浏览 2 评论 0原文

有没有跨平台的方法来检查我的Python脚本是否以管理员权限执行? 不幸的是,os.getuid() 仅适用于 UNIX,在 Windows 下不可用。

Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, os.getuid() is UNIX-only and is not available under Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如歌彻婉言 2024-07-31 23:09:44
import ctypes, os
try:
 is_admin = os.getuid() == 0
except AttributeError:
 is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

print is_admin
import ctypes, os
try:
 is_admin = os.getuid() == 0
except AttributeError:
 is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

print is_admin
冷清清 2024-07-31 23:09:44

这是我根据接受的答案创建的实用函数:

import os
import ctypes

class AdminStateUnknownError(Exception):
    """Cannot determine whether the user is an admin."""
    pass


def is_user_admin():
    # type: () -> bool
    """Return True if user has admin privileges.

    Raises:
        AdminStateUnknownError if user privileges cannot be determined.
    """
    try:
        return os.getuid() == 0
    except AttributeError:
        pass
    try:
        return ctypes.windll.shell32.IsUserAnAdmin() == 1
    except AttributeError:
        raise AdminStateUnknownError

Here's a utility function I created from the accepted answer:

import os
import ctypes

class AdminStateUnknownError(Exception):
    """Cannot determine whether the user is an admin."""
    pass


def is_user_admin():
    # type: () -> bool
    """Return True if user has admin privileges.

    Raises:
        AdminStateUnknownError if user privileges cannot be determined.
    """
    try:
        return os.getuid() == 0
    except AttributeError:
        pass
    try:
        return ctypes.windll.shell32.IsUserAnAdmin() == 1
    except AttributeError:
        raise AdminStateUnknownError
挽你眉间 2024-07-31 23:09:44

最好检查脚本正在运行的平台(使用 sys.platform )并基于该平台进行测试,例如从另一个特定于平台的模块导入一些 hasAdminRights 函数。

在 Windows 上,您可以使用 os.access 检查 Windows\System32 是否可写,但请记住尝试检索系统的实际“Windows”文件夹路径,可能使用 pywin32。 不要硬编码。

It's better if you check which platform your script is running (using sys.platform) and do a test based on that, e.g. import some hasAdminRights function from another, platform-specific module.

On Windows you could check whether Windows\System32 is writable using os.access, but remember to try to retrieve system's actual "Windows" folder path, probably using pywin32. Don't hardcode one.

情痴 2024-07-31 23:09:44

尝试执行任何需要管理员权限的操作,并检查是否失败。

但这只适用于某些事情,你想做什么?

Try doing whatever you need admin rights for, and check for failure.

This will only work for some things though, what are you trying to do?

站稳脚跟 2024-07-31 23:09:44

管理员组成员身份(域/本地/企业)是一回事。

定制您的应用程序以不使用一揽子特权并设置细粒度的权限是一个更好的选择,特别是在交互式使用应用程序的情况下。

测试特定的命名权限(se_shutdown se_restore 等),文件权限是更好的选择并且更容易诊断。

Administrator group membership (Domain/Local/Enterprise) is one thing..

tailoring your application to not use blanket privilege and setting fine grained rights is a better option especially if the app is being used iinteractively.

testing for particular named privileges (se_shutdown se_restore etc), file rights is abetter bet and easier to diagnose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文