简体   繁体   English

期望在python3中抛出错误为“必须在str中,而不是字节”

[英]expect in python3 is throwing error as “must be in str , not bytes”

I am migrating my code to python 3.4.3. 我正在将我的代码迁移到python 3.4.3。 This code works fine in python 2.4.3. 这段代码在python 2.4.3中工作正常。 but here it throws error in python 3.4.3. 但这里它在python 3.4.3中抛出错误。 should I use anything different from expect ? 我应该使用与预期不同的东西吗? Here is my code snippet which gets the error: 这是我的代码片段,它会收到错误:

   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(">")

The error what I get is : 我得到的错误是:

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 wants to log bytes, not decoded strings. pexpect想记录字节,而不是解码字符串。 You can just let it do that: 你可以让它做到这一点:

telconn.logfile = sys.stdout.buffer

sys.stdout defaults to expecting strings. sys.stdout默认为期待字符串。 Internal buffer is happy with bytes. 内部缓冲区很满意字节。

The accepted answer is correct in that you're currently trying to log bytes and not string. 接受的答案是正确的,因为您当前正在尝试记录字节而不是字符串。 But at least as of pexpect version 4.6, you can provide the encoding argument to spawn . 但至少从pexpect版本4.6开始,您可以spawn提供encoding参数 So use: 所以使用:

telconn=pexpect.spawn('telnet 10.24.12.83', encoding='utf-8')

Then all communication with the spawned terminal is automatically decoded using utf-8, and you'll be able to use telconn.logfile = sys.stdout . 然后使用utf-8自动解码与生成的终端的所有通信,并且您将能够使用telconn.logfile = sys.stdout

As of this bug report you should use spawnu instead of spawn to write unicode instead of bytes. 在此错误报告中,您应该使用spawnu而不是spawn来编写unicode而不是字节。 You might not have known that spawn uses binary logfiles, and spawnu uses unicode logfiles. 您可能不知道spawn使用二进制日志文件,而spawnu使用unicode日志文件。

将第53行更改为

login1=telconn.expect(">"+"key to proceed.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python3 中解码字节字符串时出错 [TypeError: must be str, not bytes] - Error decoding byte string in Python3 [TypeError: must be str, not bytes] ElementTree TypeError“write()参数必须是str,而不是Python3中的字节” - ElementTree TypeError “write() argument must be str, not bytes” in Python3 Python3 类型错误:“值”必须是 str 或字节的实例,而不是元组 - Python3 TypeError: 'value' must be an instance of str or bytes, not a tuple Python错误:参数1必须为缓冲区或字节,不能为str - Python Error: Argument 1 must be buffer or bytes not str Python sendto错误TypeError:必须为str,而不是字节 - Python sendto Error TypeError: must be str, not bytes Paperboy 抛出错误:TypeError:JSON 对象必须是 str,而不是“字节”? - Paperboy throwing error: TypeError: the JSON object must be str, not 'bytes'? Python3 str(),字节和unicode - Python3 str(), bytes, and unicode Python3 configparser从第一个arg开始必须是字节或字节元组,而不是str - Python3 configparser startswith first arg must be bytes or a tuple of bytes, not str Python3 错误:initial_value 必须是 str 或 None,带有 StringIO - Python3 error: initial_value must be str or None, with StringIO TypeError:必须是str,而不是字节Error - TypeError: must be str, not bytes Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM