windows Python - 检查用户是否具有管理员权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2946746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - checking if a user has administrator privileges
提问by Kefka
I'm writing a little program as a self-learning project in Python 3.x. My idea is for the program to allow two fields of text entry to the user, and then plug the user's input into the value of two specific registry keys.
我正在用 Python 3.x 编写一个小程序作为自学项目。我的想法是让程序允许用户输入两个文本字段,然后将用户的输入插入到两个特定注册表项的值中。
Is there a simple way to make it check if the current user can access the registry? I'd rather it cleanly tell the user that he/she needs administrator privileges than for the program to go nuts and crash because it's trying to access a restricted area.
有没有一种简单的方法可以检查当前用户是否可以访问注册表?我宁愿它清楚地告诉用户他/她需要管理员权限,而不是让程序因为它试图访问受限区域而发疯和崩溃。
I'd like it to make this check as soon as the program launches, before the user is given any input options. What code is needed for this?
我希望它在程序启动后立即进行此检查,然后再为用户提供任何输入选项。这需要什么代码?
Edit: in case it isn't obvious, this is for the Windows platforms.
编辑:如果不明显,这是针对 Windows 平台的。
回答by tahoar
I needed a simple, Windows/Posix solution to test if a user has root/admin privileges to the file system without installing an 3rd party solution. I understand there are vulnerabilities when relying on environment variables, but they suited my purpose. It might be possible to extend this approach to read/write the registry.
我需要一个简单的 Windows/Posix 解决方案来测试用户是否具有文件系统的 root/admin 权限,而无需安装 3rd 方解决方案。我知道依赖环境变量时存在漏洞,但它们符合我的目的。可以扩展这种方法来读/写注册表。
I tested this with Python 2.6/2.7 on WinXP, WinVista and Wine. If anyone knows this will not work on Pyton 3.x and/or Win7, please advise. Thanks.
我在 WinXP、WinVista 和 Wine 上使用 Python 2.6/2.7 对此进行了测试。如果有人知道这不适用于 Pyton 3.x 和/或 Win7,请告知。谢谢。
def has_admin():
import os
if os.name == 'nt':
try:
# only windows users with admin privileges can read the C:\windows\temp
temp = os.listdir(os.sep.join([os.environ.get('SystemRoot','C:\windows'),'temp']))
except:
return (os.environ['USERNAME'],False)
else:
return (os.environ['USERNAME'],True)
else:
if 'SUDO_USER' in os.environ and os.geteuid() == 0:
return (os.environ['SUDO_USER'],True)
else:
return (os.environ['USERNAME'],False)
回答by Alex Martelli
回答by Eloff
Here's a short article on how to determine if an application requires elevated privileges.
这是一篇关于如何确定应用程序是否需要提升权限的简短文章。
You can use pywin32 or ctypes to do the CreateProcess() call.
您可以使用 pywin32 或 ctypes 来执行 CreateProcess() 调用。
I suggest ctypes since it comes standard in python now, and there is a good example of using CreateProcess with ctypeshere.
我建议使用 ctypes,因为它现在在 python 中是标准的,这里有一个使用 CreateProcess 和 ctypes的很好的例子。
回答by nosklo
Most straightforward way is try to change the key at the beginning, maybe to a stub value - if that fails, catch the error and tell the user.
最直接的方法是尝试在开始时更改密钥,可能更改为存根值 - 如果失败,则捕获错误并告诉用户。