如何在python脚本中自动输入htpasswd密码

2024-03-29 08:56:54 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的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

It is working great but the only improvement I want is, how to automate the password entry it ask for after running the scripts that should be set from some variable value


Tags: 文件the函数密码executereturnisdef
1条回答
网友
1楼 · 发布于 2024-03-29 08:56:54

htpasswd有{a1}:

Read the password from stdin without verification (for script usage).

所以:

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

相关问题 更多 >