【发布时间】:2017-08-23 07:34:12
【问题描述】:
这是我的 python 函数,它创建 htpasswd 文件并在其中添加用户名和密码。
def __adduser(self, passfile, username):
command=["htpasswd", "-c", passfile, username]
execute=subprocess.Popen(command, stdout=subprocess.PIPE)
result=execute.communicate()
if execute.returncode==0:
return True
else:
#will return the Exceptions instead
return False
它工作得很好,但我想要的唯一改进是,如何 在运行应该从某个变量值设置的脚本后自动输入它要求的密码
【问题讨论】:
-
我宁愿查看现有的 Python 实现/包装器,也不愿在这里尝试通过 CLI 子进程...pypi.python.org/pypi/htpasswd
-
我在使用 htpasswd 时遇到的问题是,如果文件已经存在,则会附加新的用户名。我只想要一个带有单个条目的文件。 htpasswd 无法满足
-
您基本上只是想删除文件,然后创建一个新文件。这就是
-c标志的作用。你可以在 Python 中完美地做到这一点,即使 PyPi htpasswd 库没有为此提供特定选项。 -
这就是我在那里使用 -c 而没有使用模块的原因
标签: python subprocess .htpasswd