[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > I'm trying to perform following operation from inside the python > script > 1. Open a shell ( start a process ) > 2. Send command1 to the process > 3. Get output from the process > 4. Send command2 to the process > 5. Get output from the process > ...... > > > Following is sample code : > > from subprocess import * > p2 = Popen('python',stdin=PIPE,stdout=PIPE,universal_newlines=True) > for i in range(10): > p2.stdin.write('print 10'+'\n') > o,e = p2.stdout.readline() > print o,e > > It seems that stdout.readline() is a blocking read and it just gets > stuck their.. > How to fix this ..
If you are working under linux/some-unix-like-os then use the python expect module which is for exactly this sort of thing. http://www.noah.org/wiki/Pexpect Pexpect is a pure Python expect-like module. Pexpect makes Python a better tool for controlling other applications. Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python. Unlike other Expect-like modules for Python, Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module. The Pexpect interface was designed to be easy to use. You'll never get it to work with subprocess like this because of the buffering. -- Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list