p = Popen(cmd, bufsize=1024, stdin=PIPE, stdout=PIPE, stderr=PIPE,
    close_fds=True)
p.stdin.close()
print(p.stdout.read())
print(p.stderr.read())
Processing messages from a child process thorough stderr and stdout with Python