#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Subprocess - then read - then write to same shell?


    Please can someone explain how to do this:

    I want to make some commands in a shell - read the output and based on the output make another input in the same shell

    Code:
    import subprocess
    p = subprocess.Popen('some_program', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
    out, err = p.communicate()
    
    if "abcdefg" in out:
        p.communicate(input = ' -c -d 1234')
        out, err = p.communicate()
        if "xyz" in out:
            print "I have been able to open a shell, read the output and give another command to the same shell"
    I have looked at readlines, stdin.write etc but I am lost as to what the solution will be. It seems that as soon as I 'read' the output the process is then closed preventing me from giving any further inputs. If I open a new shell then its no good - because the 2nd commands must come after the 1st (if it meets a condition).
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Others and I solve this problem by searching the subprocess output for a known prompt. At that point we know the shell is ready for new input.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Others and I solve this problem by searching the subprocess output for a known prompt. At that point we know the shell is ready for new input.
    But how do you do that? Once you have read the output it appears to then close it - so that you can't write anything else. What command do you use to search and what are you searching in?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    I can't help it if your program terminates. oh, yes, you could run it in a shell loop. Anyway, I helped some dude or dudette interact with a chess engine about a year ago. Click this underlined text. There are other examples, tell google to search with
    Code:
    site:forums.devshed.com/python-programming-11/ subprocess b49p23tivg
    ha ha there are many hits.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Much appreciated for your quick and informative replies!

    I have looked through but I'm still doubtful that subprocess can solve my problem - it seems once you search or read the output that the pipe is then closed.

    I have however discovered the addon module 'pexpect'.

    For example the following works:
    Create two python files as below - the first must be called login.py (only because we call that specific file from the test.py main file - which is what we run). Test both cases by first entering correct password (password) and secondly entering an incorrect password ( =! password)

    Code:
    \\\\\\\\\login.py \\\\\\\\\
    passw = raw_input('Enter PASS: ')
    if passw == 'password':
        raw_input('You are logged in')
    else:
        raw_input('Incorrect login')
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    
    /////// test.py /////////
    import pexpect
    
    child = pexpect.spawn('python login.py')
    index = child.expect ('PASS')
    if index==0:
        password = raw_input('Please enter your password: ')
        child.sendline (password)
    
    index = child.expect (['logged in', 'Incorrect'])
    if index ==0:
        print "Test output ===> you are logged in"
    if index ==1:
        print "Test output ===> your password was incorrect"
    
    raw_input('\nPress any key to quit')
    \\\\\\\\\\\\\\\\\
    I will experiment on my main set up to see if this will do its job in the project im working on.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Yes, the above works well when applied to my main project. This has helped me to reach a milestone today!

    thanks for comments

IMN logo majestic logo threadwatch logo seochat tools logo