title: Reading stdin line at a time in Python
body: <p>I am trying to test a particular chat program by sending a series of commands and capturing the output. </p>
  
  <p>How can I pass:</p>
  
  <pre><code>sleep 2
  echo test
  sleep 2
  echo test1
  </code></pre>
  
  <p>I have tried this:</p>
  
  <pre><code>(sleep 2; echo test; sleep 2; echo test1) | python3 test.py
  </code></pre>
  
  <p>but it just prints the first part while for the second I get nothing. It enters into an endless loop instead.</p>
  
  <p>the python program's code is:</p>
  
  <pre><code>import sys, select
  
  while True:
    socket_list = [sys.stdin]
    read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [])
    for sock in read_sockets:
      message = sys.stdin.readline()
      sys.stdout.write("&gt; %s: ")
      sys.stdout.flush()
  </code></pre>
  
  <p>I should mentioned that this is not the complete program but it is the part where it helps recreate the exact same effect.</p>