文章标签 » expect

backcup:unbuffer implementation in python

#!/usr/bin/env python
import os,sys,pty
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)

pid, fd = pty.fork()
if pid < 0:
    print >>sys.stderr, 'fork error'
#child
elif pid == 0:
    os.execvp(sys.argv[1], sys.argv[1:])
#parent
else:
    while True:
        try:
            s = os.read(fd, 1)
        except OSError, e:
            break
        if s == '':
            break
        sys.stdout.write(s)
    os.wait()