I have been writing some automation code a few dozen websites, and I wanted to generate SSL keys, and I couldn’t get the `subprocess` call to work properly, so I thought I would post the final solution. The argument to `communicate()` is critical.
import subprocess
filename = 'my.key'
numbits = 2048
cmd = ['openssl', 'genrsa', '-out', filename, str(numbits), '-passin', 'stdin']
p = subprocess.Popen(
cmd,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell=False
)
out, err = p.communicate('\n')
