title: Error message when running telnet in python shell
body: <p>Below is a minimal example of my code, which If you run, should recreate the problem I have. One thing I have encounter that the error does not appear if my raspberry pi (the thing I am using teller to connect to) is turned off or the internet cable is unplugged.</p>
  
  <pre><code>#IMPORTS
  import time
  import telnetlib
  import sys
  import getpass
  
  
  #TELNET
  user = input("Please Enter Your Username: ")
  time.sleep(0.4)
  pass_ = input("Please Enter Your Password: ")
  
  bot = telnetlib.Telnet("192.168.1.128")
  bot.read_until("login: ".encode(),  timeout=None)
  bot.write(user + "\n")
  bot.read_until("Password: ".encode(),  timeout=None)
  bot.write(pass_ + "\n")
  bot.write("cd PiBits/ServoBlaster")
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/kiancross/Desktop/PROJECTS/RASPBERRY_PI/ROBOT/CONTROLLER_GUI/RPi_BOT_CONTROLLER.py", line 17, in &lt;module&gt;
      bot.write(user + "\n")
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/telnetlib.py", line 289, in write
      if IAC in buffer:
  TypeError: 'in &lt;string&gt;' requires string as left operand, not bytes
  </code></pre>
  
  <p>I was running the following code which you can see in <a href="https://stackoverflow.com/questions/23221123/why-do-i-get-this-error-message-when-using-telnet-in-python-shell">this</a> post but as you can see from the answer I changed it to the code above which resulted in the above error message.</p>
  
  <p>I should say that the code is no where near finished and a lot of things are just to test until I get telnet working!</p>
  
  <p>I have tried the fix for the older version of python mentioned in <a href="http://mechanix-tips.blogspot.co.uk/2008/12/python-telnetlib-typeerror-in-requires.html" rel="nofollow noreferrer">this</a> post.</p>
  
  <p>I have used <code>print(type(user))</code> to see what kind the variables are and they are strings. I have tried <code>bot.write((user + "\n").encode(latin-1))</code> as shown in this fix, but the error message still appears. I have also tried encoding in unicode. I understand that part of that line is encoded wrong but I don't know if it is the variables or the <code>"\n"</code>. I have tried doing <code>bot.write(user + b"\n")</code> but this get rid of the error.</p>
  
  <p>If anybody has any other methods that could stop this error, I would greatly appreciate them.</p>
  
  <p>Thanks</p>
  
  <p>P.S Im running on python 3.4.0</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have tried this:</p>
  
  <pre><code>bot = telnetlib.Telnet("192.168.1.128")
  bot.write(user + "\n".encode('ascii'))
  bot.read_until(b"Password: ")
  bot.write((pass_ + "\n").encode('ascii'))
  </code></pre>
  
  <p>But I still get the same error.</p>
  
  <p>When I do:</p>
  
  <pre><code>bot = telnetlib.Telnet("192.168.1.128")
  bot.read_until(b"login: ")
  bot.write(user + b"\n")
  bot.read_until(b"Password: ")
  bot.write(pass_ + b"\n")
  </code></pre>
  
  <p>I get a different error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/kiancross/Desktop/PROJECTS/RASPBERRY_PI/ROBOT/CONTROLLER_GUI/RPi_BOT_CONTROLLER.py", line 23, in &lt;module&gt;
      bot.write(user + b"\n")
  TypeError: Can't convert 'bytes' object to str implicitly
  </code></pre>
  
  <p>All the error seem to say that It needs to be a string but my variables are already strings.</p>
  
  <p>Thanks</p>