If the child process reads from stderr (note: normally stderr is opened for output):
#!/usr/bin/env python """Read from *stderr*, write to *stdout* reversed bytes.""" import os os.write(1, os.read(2, 512)[::-1])
then you could provide a pseudo-tty (so that all streams point to the same place), to work with the child as if it were a normal subprocess:
#!/usr/bin/env python import sys import pexpect # $ pip install pexpect child = pexpect.spawnu(sys.executable, ['child.py']) child.sendline('abc') # write to the child child.expect(pexpect.EOF) print(repr(child.before)) child.close()
Output
u'abc\r\n\r\ncba'
You could also use subprocess + pty.openpty() instead pexpect.
Or you could write a code specific to the weird stderr behavior:
#!/usr/bin/env python import os import sys from subprocess import Popen, PIPE r, w = os.pipe() p = Popen([sys.executable, 'child.py'], stderr=r, stdout=PIPE, universal_newlines=True) os.close(r) os.write(w, b'abc') # write to subprocess' stderr os.close(w) print(repr(p.communicate()[0]))
Output
'cba'