【问题标题】:how to automate the htpasswd password entry in python scripts如何在 python 脚本中自动输入 htpasswd 密码
【发布时间】: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


【解决方案1】:

htpasswd 拥有an -i option

从标准输入读取密码,无需验证(用于脚本)。

所以:

def __adduser(self, passfile, username, password):
    command = ["htpasswd", "-i", "-c", passfile, username]
    execute = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    execute.communicate(input=password)
    return execute.returncode == 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 2018-07-07
    • 2012-05-23
    相关资源
    最近更新 更多