@mammique: "Hi all,
I'm having a problem with subprocess.Popen. It seems that its unable to
capture the pg_dump's standard inputs & outputs in a non-shell mode:
from subprocess import Popen, PIPE
# fire pg_dump in order to read data from the file object pgsql.stdout
pgsql = Popen(['/usr/bin/pg_dump',
               'host', 'localhost',
               'password',
               'username',
               'mammique'],
              stdin=PIPE,
              stderr=PIPE,
              stdout=PIPE,
              shell=True)
# pg_dump prompts for password so I inject it in stdin.
pgsql.stdin.write('MY_PASSWORD' + '\n')
In the shell mode (shell=True) pipes works but the args (username, etc.)
are omitted. If I turn to shell=False the arguments are passed to
pg_dump but subprocess.Popen is no longer able to capture the standard
inputs & outputs, the password prompt appears on the TTY instead and
doesn't take the input written in stdin. It seems that subprocess.Popen
has only this problem with pg_dump, other interactive command lines
seems to be correctly handled.
Any lead?
Thanks,
Camille."
    @clp2: "On Sun, Nov 14, 2010 at 10:50 AM, Camille Harang <email_address> wro=
te:
<quote>
)
Quoting <url> , emphasis mine:
"""
On Unix, with shell=3DTrue: [...] If args is a sequence, ***the first
item*** specifies the command string, and any additional items will be
treated as additional arguments ***to the shell itself***.
"""
So if you're using shell=3DTrue, pass a single string rather than a
tokenized list. That is to say:
command =3D "/usr/bin/pg_dump host localhost password username mammiq=
ue"
pgsql =3D Popen(command, stdin=3DPIPE, stderr=3DPIPE, stdout=3DPIPE, shell=
=3DTrue)
Cheers,
Chris
<url>"
    @mammique: "Hi Chris, thanks for your reply.
Chris Rebert a écrit :
<quote>
Yes, I read that, that's why I'd like to avoid shell=True, but if I do
shall=False subprocess.Popen is no longer able to catch the standard
inputs & outputs of pg_dump (but it does on other commands that has a
prompt, e.g. 'passwd')
That's the problem :-/
Regards,
Camille.
The Good, the Bad and the Ugly under Creative Commons!
<url>"
    @mammique: "Tim Harig a �crit :
<quote>
Ha, indeed, I suspected that pg_dump didn't behave normally, security
matters might be an explanation, I'll dig in that way, thanks a lot.
Regards,
Camille."
    @nobody: "<quote>
Indeed, the getpass() function exists[1] for this purpose:
       The  getpass() function opens /dev/tty (the controlling terminal of the
       process), outputs the string prompt, turns off echoing, reads one  line
       (the  "password"),  restores  the  terminal  state  and closes /dev/tty
       again.
[1] Or existed. It was dropped from the Unix standard in POSIX.1-2001, but
most systems still provide it."
{'source': 'USENET', 'other_information': '{"reply_count": 4, "forum_name": "comp.lang.python.20150208.mbox"}', 'file': 'data/output/usenet-comp.lang.parquet'}