views:

52

answers:

1

Hi,

I'm having troubles getting this to work. Basically I have a python program that expect some data in stdin, that is reading it as sys.stdin.readlines() I have tested this and it is working without problems with things like echo "" | myprogram.py

I have a second program that using the subprocess module calls on the first program with the following code

    proc = subprocess.Popen(final_shell_cmd,
                stderr=subprocess.PIPE, stdout=subprocess.PIPE,
                shell=False), env=shell_env)
    f = ' '.join(shell_cmd_args)
    #f.append('\4')
    return proc.communicate(f)

The second program is a daemon and i have discovered that the second program works well as long as I hit ctrl-d after calling it from the first program.

So it seems there is something wrong with subprocess not closing the file and my first program expecting more input when nothing more should be sending.

anyone has any idea how I can get this working?

The main problem here is that "shell_cmd_args" may contain passwords and other sensitive information that we do not want to pass in as the command name as it will show in tools like "ps".

+2  A: 

You want to redirect the subprocess's stdin, so you need stdin=subprocess.PIPE.

You should not need to write Control-D ('\4') to the file object. Control-D tells the shell to close the standard input that's connected to the program. The program doesn't see a Control-D character in that context.

Daniel Stutzbach
excellent that is exactly what I was missing Thank you!as for the '\4' the formatting got it off. It was commented out as a test, I was sure it didn't work but I left it in just in case I was sending the wrong char.
Jorge Vargas