Skip to content Skip to sidebar Skip to footer

Writing To Stdin, Access Denied

I'm trying to write a python script that starts a subprocess, and writes to the subprocess stdin. Does some tests on teh output and then writes more commands to stdin. I have tried

Solution 1:

If you could tell us more about that program, maybe someone knowing this program could try to explain better how it works in particular.

Nevertheless, what you describe

starts a subprocess, and writes to the subprocess stdin. Does some tests on teh output and then writes more commands to stdin.

does not match your code.

Your code prints something to our own stdout, displaying band, and then does a "one-shot" communication with the subprocess.

To be clear about that, p.communicate() writes all it gets to the subprocess, closes its stdin and reads out whatever it gets from stdout and stderr.

Thus it is incompatible with what you desire: write, read, write again.

So you'll have to craft that on your own.

If the chunks you write are small enough to be guaranteed to fit into the pipe buffer, it is simple: just write the commands (don't forget the trailing \n) and read.

But be aware! Don't read more than you really have, or your reading might block.

Thus, work with non-blocking IO or with select.select().

If you need more information about the one or other, there are other answers here on SO which cover these subjects. The other day I wrote one which might help you.

Solution 2:

This worked for some reason, passing in the command in the same line. Then call this function for every command I want.

  p = subprocess.Popen(["/path/to/program", '-c', '-', cmd_here],
  stdout=subprocess.PIPE) 
  proc_stdout, proc_stderr = proc.communicate()
  proc.wait()
  #print stuff

Post a Comment for "Writing To Stdin, Access Denied"