我正在将我的代码迁移到 python 3.4.3。此代码在 python 2.4.3 中运行良好。但这里它在 python 3.4.3 中抛出错误。 我应该使用与 expect 不同的东西吗? 这是我得到错误的代码片段:
telconn=pexpect.spawn('telnet 10.24.12.83')
telconn.logfile = sys.stdout
login=telconn.expect([":","key to proceed.",">"])
if login==0:
telconn.send("user1" + "\r")
telconn.expect(":")
telconn.send("paswd1" + "\r\r\r\r\n\n\n")
login1=telconn.expect([">","key to proceed."])
if login1==0:
print("nothing")
elif login1==1:
telconn.expect("key to proceed.")
telconn.send ("\003")
telconn.expect(">")
if login==1:
telconn.send ("\003")
telconn.expect(">")
print("ctlc")
elif login==2:
telconn.send("\n\r")
telconn.expect(">")
我得到的错误是:
Traceback (most recent call last):
File "cleanup1.py", line 128, in <module>
Connect()
File "cleanup1.py", line 53, in Connect
login=telconn.expect([":","key to proceed.",">"])
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 315, in expect
timeout, searchwindowsize, async)
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 339, in expect_list
return exp.expect_loop(timeout)
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/expect.py", line 97, in expect_loop
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/pty_spawn.py", line 455, in read_nonblocking
return super(spawn, self).read_nonblocking(size)
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 157, in read_nonblocking
self._log(s, 'read')
File "/corp/global/install-dependent/python/3.4.3/lib/python3.4/site-packages/pexpect/spawnbase.py", line 115, in _log
self.logfile.write(s)
TypeError: must be str, not bytes
最佳答案
pexpect
想要记录字节,而不是解码的字符串。你可以让它这样做:
telconn.logfile = sys.stdout.buffer
sys.stdout
默认为预期字符串。内部缓冲区对字节很满意。
关于python - python3 中的 pexpect.expect() 抛出错误 "must be in str , not bytes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330424/