python3 中的 pexpect.expect() 抛出错误 "must be in str , not bytes"
pexpect.expect() in python3 is throwing error as "must be in str , not bytes"
我正在将我的代码迁移到 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 ("[=13=]3")
telconn.expect(">")
if login==1:
telconn.send ("[=13=]3")
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
将第 53 行更改为
login1=telconn.expect(">"+"key to proceed.")
pexpect
想要记录字节,而不是解码的字符串。你可以让它这样做:
telconn.logfile = sys.stdout.buffer
sys.stdout
默认为期望字符串。内部缓冲区对字节很满意。
从这个 bug report 开始,您应该使用 spawnu
而不是 spawn
来编写 unicode 而不是字节。您可能不知道 spawn
使用二进制日志文件,而 spawnu
使用 unicode 日志文件。
接受的答案是正确的,因为您当前正在尝试记录字节而不是字符串。但至少从 pexpect 版本 4.6 开始,你可以 provide the encoding
argument to spawn
。所以使用:
telconn=pexpect.spawn('telnet 10.24.12.83', encoding='utf-8')
然后与衍生终端的所有通信都使用 utf-8 自动解码,您将能够使用 telconn.logfile = sys.stdout
。
我正在将我的代码迁移到 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 ("[=13=]3")
telconn.expect(">")
if login==1:
telconn.send ("[=13=]3")
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
将第 53 行更改为
login1=telconn.expect(">"+"key to proceed.")
pexpect
想要记录字节,而不是解码的字符串。你可以让它这样做:
telconn.logfile = sys.stdout.buffer
sys.stdout
默认为期望字符串。内部缓冲区对字节很满意。
从这个 bug report 开始,您应该使用 spawnu
而不是 spawn
来编写 unicode 而不是字节。您可能不知道 spawn
使用二进制日志文件,而 spawnu
使用 unicode 日志文件。
接受的答案是正确的,因为您当前正在尝试记录字节而不是字符串。但至少从 pexpect 版本 4.6 开始,你可以 provide the encoding
argument to spawn
。所以使用:
telconn=pexpect.spawn('telnet 10.24.12.83', encoding='utf-8')
然后与衍生终端的所有通信都使用 utf-8 自动解码,您将能够使用 telconn.logfile = sys.stdout
。