python equivalent of qx in perl-user/password prompt included
Question: In Perl, if I have execute a script and pass a password to it programatically,
I would do this:

    
    
    my $result = qx { "Calling some script which prompts for a user and  password" <<EOF
    administrator
    password
    EOF
    };
    

It executes the following while capturing its output:

    
    
    /bin/sh -c ' "Calling some script which prompts for a user and  password" <<EOF
    administrator
    password
    EOF
    '
    

May I know the equivalent of this in Python?


Answer: If I udnerstand your question correctly, you're trying to start an external
script within its own process, and send that script some data - a password -
via its standard input.

In Python, this is done using the
[`subprocess`](https://docs.python.org/2/library/subprocess.html#module-
subprocess) module. This module runs external scripts and has a `stdin`,
`stdout` and `stderr` parameters.

For example, suppose that the script is `md5sum`. You would like to send it
the password `secret`:

    
    
    >>> import subprocess as sp
    >>> p = sp.Popen('md5sum', stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
    >>> p.communicate(input="secret")                   # <-- Your password
    ('5ebe2294ecd0e0f08eab7690d2a6ee69  -\n', '')       # <-- Process output
    

`p.communicate()` returns an (`stdout`, `stderr`) tuple, which is useful for
processing the script output.

You may also find [this answer](http://stackoverflow.com/a/8475367/51197)
useful.

