[Python-Dev] Re: Could getpass use streams other than stdin?

Fredrik Lundh fredrik at pythonware.com
Fri Aug 13 08:29:58 CEST 2004


Mike Brown wrote:

> In my use case, I need to send the results of a command-line script to stdout
> after the user authenticates, and the user may be redirecting stdout, so I
> don't want the password prompt to pollute the output. If I could have the
> "Password: " show up in stderr, that would be ideal.

you can redirect sys.stdin/sys.stdout before you call getpass:

    try:
        old_stdout = sys.stdout
        sys.stdout = sys.stderr
        password = getpass.getpass(prompt)
    finally:
        sys.stdout = old_stdout

</F> 





More information about the Python-Dev mailing list