python 使用 openssl 的子进程
python subprocess with openssl
我在通过 python 的子进程将字符串传递给 openssl 命令行工具时遇到很多问题,如下所示:
process = subprocess.Popen(
["openssl", "rsa", "-in", pathFile, "-out", "id_rsa.out"],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=False
)
try:
process.communicate("some passphrase\n", timeout=2)
except:
process.kill() #openssl stays alive otherwise.
上面的代码超时(在 Popen 中有和没有 std 重定向)。我可以通过终端正常使用 openssl,但我真的需要能够 运行 这是我的 python 脚本的一部分。
如有任何帮助,我们将不胜感激。
the openssl man page 上的密码短语参数部分解释了密码短语输入机制的工作原理。为了使您的示例工作,您应该告诉 openssl 从 stdin
获取密码。以您的示例为起点,以下对我有用:
process = subprocess.Popen(
["openssl", "rsa", "-in", pathFile, "-out", "id_rsa.out", "-passin", "stdin"],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=False
)
process.communicate("passphrase\n")
我在通过 python 的子进程将字符串传递给 openssl 命令行工具时遇到很多问题,如下所示:
process = subprocess.Popen(
["openssl", "rsa", "-in", pathFile, "-out", "id_rsa.out"],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=False
)
try:
process.communicate("some passphrase\n", timeout=2)
except:
process.kill() #openssl stays alive otherwise.
上面的代码超时(在 Popen 中有和没有 std 重定向)。我可以通过终端正常使用 openssl,但我真的需要能够 运行 这是我的 python 脚本的一部分。
如有任何帮助,我们将不胜感激。
the openssl man page 上的密码短语参数部分解释了密码短语输入机制的工作原理。为了使您的示例工作,您应该告诉 openssl 从 stdin
获取密码。以您的示例为起点,以下对我有用:
process = subprocess.Popen(
["openssl", "rsa", "-in", pathFile, "-out", "id_rsa.out", "-passin", "stdin"],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=False
)
process.communicate("passphrase\n")