I am trying to pipe username and password (not to pass through command line for safety purposes) from one python script to another python script with some arguments. I was successful in perl implementing the above like this
program - perl1:
open(INPUT,"|perl my.pl $arg1 $arg2") or die "cant fork $!";
print INPUT "$username\n";
print INPUT "$password\n";
close INPUT;
program - my.pl:
while (<STDIN>) {
$user[$p]=$_; #store username and password in an array
$p++;
}
I am trying to implement the same with python like this (code below) it does not work. My 2nd program gets stuck getting the stdin from program 1. Greatly appreciate help with what i am doing wrong. I am just getting into python programming and i am stuck with this piping.
start=subprocess.Popen(["python","run.py","--arg1","arg1","--arg2","arg2"],stdin=subprocess.PIPE) #subprocess.popen to open pipe or stdout to the second script
start.stdin.write("username\n") #write the username to the stdin of run.py
start.stdin.write("password\n") #write password to stdin of run.py
start.wait() #wait till subprocess ends and proceed with the next line of code.
run.py
for line in sys.stdin: #also tried sys.stdin.readlines()
print line
Nothing happens. The screen is stuck for a long time over there and no input is read and printed. Is there a better way to do what i am trying to implement or what am i doing wrong here.
PS: I did a similar piping using subprocess.popen with python and Java it worked fine. unfortunately with python and python i get this error.
Thanks