touch my body touch my body - 1 year ago 175
Python Question

Print password prompt using Python's subprocess

If I run a terminal command from Python using

, how do I print the password prompt?

I have a Python script that asks users to authenticate via Kerberos. My program is basically this:

import subprocess

def kinit():
kinit = "kinit -f"
prompt = subprocess.check_output(kinit, shell=True)
print prompt ## echoes "Password for" AFTER pw input

if __name__=="__main__":

When this script is launched from the Terminal, the program waits for password input (which can be input via the command line), but it only displays the "Password for" prompt AFTER the user inputs their password.

Do you know how I can display the "Password for" before the Python script waits for them to input their password?

Answer Source

Turns out you can't. subprocess will not return output in the middle of a process, and the process will not complete until you input user input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download