6

I am struggling to use subprocesses with python. Here is my task:

  1. Start an api via the command line (this should be no different than running any argument on the command line)
  2. Verify my API has come up. The easiest way to do this would be to poll the standard out.
  3. Run a command against the API. A command prompt appears when I am able to run a new command
  4. Verify the command completes via polling the standard out (the API does not support logging)

What I've attempted thus far:
1. I am stuck here using the Popen. I understand that if I use subprocess.call("put command here") this works. I wanted to try to use something similar to:

import subprocess

def run_command(command):
  p = subprocess.Popen(command, shell=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT)

where I use run_command("insert command here") but this does nothing.

with respect to 2. I think the answer should be similar to here: Running shell command from Python and capturing the output, but as I can't get 1. to work, I haven't tried that yet.

2 Answers 2

9

To at least really start the subprocess, you have to tell the Popen-object to really communicate.

def run_command(command):
    p = subprocess.Popen(command, shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
    return p.communicate()
Sign up to request clarification or add additional context in comments.

Comments

7

You can look into Pexpect, a module specifically designed for interacting with shell-based programs.

For example launching a scp command and waiting for password prompt you do:

child = pexpect.spawn('scp foo [email protected]:.')
child.expect ('Password:')
child.sendline (mypassword)

See Pexpect-u for a Python 3 version.

1 Comment

Just to add that, AFAICT, the subprocess module is really geared towards tasks that run with their initial input and then complete (like calling out to a function). If you need an ongoing back and forth (e.g. for an interactive terminal utility) then Pexpect is definitely the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.