title: Python telnet connection closes
body: <p>I'm trying to send some commands via telnet to a server. This has been done by hand many times, but I was asked to automate it using python.</p>
  
  <p>My code starts like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>import telnetlib
  
  HOST = "xxx.xxx.xxx.xxx"
  PORT = xx
  
  print("connecting...")
  tn = telnetlib.Telnet(HOST, PORT)
  tn.read_until(b"250")
  print("connection established!\nsending ehlo...")
  tn.write(b"EHLO test.com")
  tn.read_until(b"250")
  print("response received")
  ...
  </code></pre>
  
  <p>If I enter the commands by hand, it works. But the script aborts after a few minutes with the following output:</p>
  
  <pre><code>connecting...
  connection established!
  sending ehlo...
  Traceback (most recent call last):
    File "telnet_test.py", line 11, in &lt;module&gt;
      tn.read_until(b"250")
    File "/usr/lib/python3.6/telnetlib.py", line 327, in read_until
      return self.read_very_lazy()
    File "/usr/lib/python3.6/telnetlib.py", line 403, in read_very_lazy
      raise EOFError('telnet connection closed')
  EOFError: telnet connection closed
  </code></pre>
  
  <p>According to the output my telnet connection got closed, but I can't see why!?
  Maybe important: After launch the output says <code>connecting...</code> for a few minutes, and then the rest of the output including the exception get's printed.
  Is the <code>read_until()</code> function blocking longer than necessary, so my connection timouts? And how do I solve this?</p>