'Subprocess direct interaction with the child's stdin and stdout

I do the following:

from subprocess import Popen, PIPE, STDOUT
p = Popen(
    r'qemu-system-arm -machine vexpress-a15 -cpu cortex-a15 -m 512M -kernel xxxxx -append "console=ttyAMA0 root=/dev/xxxxx ro rootfstype=ext4" -serial stdio -dtb xxxxx -drive if=sd,cache=writeback,file=xxxxx -net nic -net user,hostfwd=tcp::xxxxx-:xx -display none -initrd xxxxx',
    bufsize=0, stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True, text=True,
)
for line in iter(p.stdout.readline,""):
    if line:
        print(line.replace('\n', ''))
        if line.startswith('Login:'):
            p.stdin.write('root\n\r')
            p.stdin.flush()
        if line.startswith('Password:'):
            p.stdin.write('root\n\r')
            p.stdin.flush()
    else:
        break
testresult = p.communicate()[0]
print(testresult)

However, impossible to introduce the password, I get:

Password: 
Login timed out after 60 seconds.

I've scoured the internet for a solution and so far nothing... I'm running the script on Windows, so I'm not using pexpect, and anyway winpexpect or wexpect doesn't work either. I'm wondering: is there a simple and clean solution using the subprocess module that won't hung or that writes the password?.. After authenticating I still need to pass more commands. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source