title: How to print telnet response line by line?
body: <p>Is it possible to print the telnet response line by line, when a command executed over telnet keeps on responding over console ?</p>
  
  <p>Example: I have executed a command (to collect logs), It keeps on displaying logs on console window. Can we read the response line by line &amp; print it , without missing any single line ?</p>
  
  <p>Below snippet writes the log, but only after certain specified time. If I stop the service/script (CTRL-C) in between, that doesn't write anything. </p>
  
  <pre><code>import sys
  import telnetlib
  import time
  
  
  orig_stdout = sys.stdout
  f = open('outpuy.txt', 'w')
  sys.stdout = f
  
  try:
          tn = telnetlib.Telnet(IP)
          tn.read_until(b"pattern1")
          tn.write(username.encode('ascii') + b"\n")
          tn.read_until(b"pattern2")
          tn.write(command1.encode('ascii') + b"\n")
          z = tn.read_until(b'abcd\b\n',600)
          array = z.splitlines( )
  except:
          sys.exit("Telnet Failed to ", IP)
  
  for i in array:
          i=i.strip()
          print(i)
  
  sys.stdout = orig_stdout
  f.close()
  </code></pre>