title: Python - integers as input piped to subprocess.Popen
body: <p>I have a list containing integers and want to direct it to a shell command that creates a histogram from it (see <a href="https://github.com/bitly/data_hacks/blob/master/data_hacks/histogram.py" rel="nofollow noreferrer">here</a>). </p>
  
  <p>For now, let's mimick the histogram generating script by a simple shell script <code>echo_script.sh</code> that echoes all the input:</p>
  
  <pre><code>#/bin/sh
  echo $@
  </code></pre>
  
  <p>From within Python, I'd like to direct the contents of a list to that command. I suppose it involves some fiddling with <code>subprocess</code>. </p>
  
  <pre><code>numbers = [1, 2, 3]
  # subprocess stuff, writing numbers to stdin of echo_script.sh process?
  </code></pre>
  
  <p>I'd like to have the following output to stdout:</p>
  
  <pre><code>1 2 3
  </code></pre>
  
  <p>Note that I'm aware of solutions that involve storing the list contents in a temporary file and piping that file to <code>echo_script.sh</code>. I'd like to avoid that.</p>
  
  <p>EDIT: The histogram.py script that I'm working with does not support reading in an arbitrary number of appended arguments, like <code>echo 1 2 3</code> would do. It only reads from <code>stdin</code>. Hence, the example above is misleading. An approach I have tried:</p>
  
  <pre><code>proc = subprocess.Popen(["wc", "-l"], stdin=subprocess.PIPE)
  proc.stdin.write(bytes([42]))
  </code></pre>
  
  <p>This outputs 1 which is the correct line count but actually the number of bytes written. So I have two more questions: </p>
  
  <ul>
  <li>How can I correctly use an array of bytes to send to <code>proc.stdin.write</code>?</li>
  <li>How can I redirect stdout of proc to the current process?</li>
  </ul>