Python telnetlib expects an error: TypeError: cannot use string pattern for byte object

I am trying to pass 2 precompiled regular expressions to the python telnetlib wait method, but I get: TypeError: you cannot use a string pattern for a byte-like object. Sample code below:

import re,sys,telnetlib tn=telnetlib.Telnet('localhost',23,10) re_list=[re.compile("login:",re.I),re.compile("username:",re.I)] print("re_list:",re_list) # Expect gets errors here index,obj,data=tn.expect(re_list,10) 

Example output below:

 python tn_exp_bug.py re_list: [<_sre.SRE_Pattern object at 0x00A49E90>, <_sre.SRE_Pattern object at 0x00A6CB60>] Traceback (most recent call last): File "tn_exp_bug.py", line 8, in <module> index,obj,data=tn.expect(re_list,10) File "c:\python33\lib\telnetlib.py", line 652, in expect return self._expect_with_select(list, timeout) File "c:\python33\lib\telnetlib.py", line 735, in _expect_with_select m = list[i].search(self.cookedq) TypeError: can't use a string pattern on a bytes-like object</pre> 

Other information: I work on Windows XP, Python version 3.3.0. I checked bugs.python.org, and there is only 1 open error for telnet that does not seem to be relevant at all.

+4
source share
1 answer

You tried to use a string pattern for a bytes object, while you should use a byte pattern:

 re.compile(b"login:",re.I),re.compile(b"username:",re.I) 
+3
source

All Articles