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

Recommended Answers

All 4 Replies

Generally you pass as arguments like this simplified code:

## program run.py
def print_args(name, passwd):
   print name
   print passwd

## calling program
import run
input_name = raw_input("Enter name ")
input_passwd = raw_input("Enter password ")
run.print_args(input_name, input_passwd)

Figured out my problem. I did not close the pipe.

Hope this helps some one :)

woooee - Appreciate your help.

I was looking to pipe for security reasons in mind.

After i had my pipe closed as explained in my previous comments things started to work as i expected :)

Generally you pass as arguments like this simplified code:

## program run.py
def print_args(name, passwd):
   print name
   print passwd

## calling program
import run
input_name = raw_input("Enter name ")
input_passwd = raw_input("Enter password ")
run.print_args(input_name, input_passwd)

Hey.........Am trying to call perl in python , But am not able to call that...Please can u provide your code here .

Am trying this sicnce from last week only ......>:(

Please help me
Pervez

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.