title: Python subprocess interact with a binary
body: <p>I want to interact with a binary by sending it some input and then print the generated output.</p>
  
  <p>The binary itself works like this:</p>
  
  <pre><code>binary.exe
  Please enter the input
  abcd
  generated_output
  </code></pre>
  
  <p>The binary reads the input using INT 21 interrupt, so there is no need to send a newline character to the binary at the end of the input. As soon as the length of input matches the desired length, the binary automatically generates the output. The length of input needs to be 10 characters.</p>
  
  <p><strong>Note:</strong> Binary reads the input one character at a time from stdin using INT 21 interrupt. So, it processes each character of the input as and when it is typed at stdin. As soon as the 10th character is typed at stdin, the output is generated.</p>
  
  <p>I would like to log the output.</p>
  
  <p>So far, I can only use subprocess check_output to get the output of a binary which says "Please enter the input" as shown below:</p>
  
  <pre><code>#! /usr/bin/python
  
  from subprocess import check_output
  import sys
  
  p = sys.argv[1]
  
  result = check_output(p, shell=True)
  
  print result
  </code></pre>
  
  <p>The above code will not return because the binary is waiting for an input to be passed to it.</p>
  
  <p>If I press "Ctrl + C" at the command line, then the first line of the binary output is printed as:</p>
  
  <p>"Please enter the input".</p>
  
  <p>I would like to pass an input to the binary after this and log the generated output to a file.</p>