0

I'm trying to write a python script that starts a subprocess, and writes to the subprocess' stdin.
Here I can write and get a result:

def get_band():
    print "band" 
    p = subprocess.Popen(["/path/to/program","-c","-"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    ran_stdout = p.communicate(input='show status')[0]
    print(ran_stdout)

However the print statement gives:

Unable to connect at 127.0.0.1, Connection refused.

If I do this the same result is displayed:

p = subprocess.Popen(["/path/to/program","-c","-"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print p[0]

If I run this command from a terminal it works fine, I can get the result back.

What is wrong with the parameters given? ["/path/to/program","-c","-"]

rypel
  • 4,686
  • 2
  • 25
  • 36
Paul
  • 5,756
  • 6
  • 48
  • 78
  • 2
    Isn't this a problem with `["/path/to/program","-c","-"]` not subprocess? – danodonovan Apr 10 '13 at 09:22
  • actually yes I think it is, will change – Paul Apr 10 '13 at 09:25
  • 1
    So, whatever 'path/to/program' is, it is producing the error you provide (it seems to be unable to connect to localhost). Surely you should be looking at correcting the program 'path/to/program'? – danodonovan Apr 10 '13 at 09:36
  • possible duplicate of [writing to stdin, access denied](http://stackoverflow.com/questions/15906833/writing-to-stdin-access-denied) – glglgl Apr 10 '13 at 09:41
  • taht turned into how to write multiple commands so I separated them – Paul Apr 10 '13 at 09:44
  • seems this is some sort of permissions error, so maybe the question cant be answered here. – Paul Apr 10 '13 at 09:45

1 Answers1

0

This worked for some reason, passing in the command in the same line.

  p = subprocess.Popen(["/path/to/program", '-c', '-', cmd_here],
  stdout=subprocess.PIPE) 
  proc_stdout, proc_stderr = proc.communicate()
  proc.wait()
Paul
  • 5,756
  • 6
  • 48
  • 78