title: Interprocess Communication Python
body: <p>I'm writing a script in Python that should communicate with a software
  "ConsoleApplication.exe"(wrote in C); this last one once started waits for a fixed
  lenght command(5 bytes) from his "stdin" and generates (after 2-3 seconds) on 
  his "stdout" an output that I should read on my Python script.</p>
  
  #
  
  <pre><code>//This is the "ConsoleApplication.c" file
  #include &lt;stdio.h&gt;
  #include &lt;function.h&gt;
  
  char* command[5];
  int main()
  {
   while(1)
   {
  
   scanf("%s\n", &amp;command);
   output = function(command);
   print("%s\n", output);
  
   }
  }
  </code></pre>
  
  #
  
  <pre><code>#this is Python code
  
  import subprocess
  #start the process
  p = subprocess.Popen(['ConsoleApplication.exe'], shell=True, stderr=subprocess.PIPE)
  #command to send to ConsoleApplication.exe
  command_to_send = "000648"
  #this seems to work well but I need to send a command stored into a buffer and if Itry 
  #to use sys.stdout.write(command_to_send)nothing will happen. The problem seem that
  #sys.stdout.write expect an object I/O FILE
  while True:
      out = p.stderr.read(1)
      if out == '' and p.poll() != None:
          break
      if out != '':
          sys.stdout.write(out)
          sys.stdout.flush()
  </code></pre>
  
  #
  
  <p>Any suggestions? How can I fix it?</p>
  
  <p>I tried to use  </p>
  
  <pre><code>stdout = p.communicate(input='test\n')[0] 
  </code></pre>
  
  <p>but Im getting the following error at runtime:
  "TypeError: 'str' does not support the buffer interface"
  I also tried this</p>
  
  <pre><code>from subprocess import Popen, PIPE, STDOUT
  
  
  p = Popen(['ConsoleApplication.exe'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
  
  out, err = p.communicate(input='00056\n'.encode())
  print(out)
  out, err = p.communicate(input='00043\n'.encode())
  print(out)
  </code></pre>
  
  <p>but I get this error:
  "ValueError: Cannot send input after starting communication"</p>