Wednesday, December 3, 2008

Python telnetlib "TypeError: 'in ' requires string as left operand" error

If you receive following error while using telnetlib in python 2.X, you are hitting http://bugs.python.org/issue1772794 which is fixed in python 3.0

  File "/usr/lib64/python2.5/telnetlib.py", line 289, in write
    if IAC in buffer:
TypeError: 'in ' requires string as left operand

A fix for that in python 2.X can be forcing encoding of string you send to telnet, i.e. instead of
  telnetconnection.write('just a test')

Use 
  telnetconnection.write('just a test').encode('latin-1'))

2 comments:

Niki said...

Thanks. Run into the same problem. Helped me a lot.

Niki

yegorov-p said...

Спасибо, минут 20 пытался найти ошибку в трех строчках своего кода =)