Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 另一个类型错误:需要类似字节的对象,而不是';str';_Python_Telnetlib - Fatal编程技术网

Python 另一个类型错误:需要类似字节的对象,而不是';str';

Python 另一个类型错误:需要类似字节的对象,而不是';str';,python,telnetlib,Python,Telnetlib,我是Python的新手,但大约从1980年开始,我就一直在(Liberty-)Basic中为好玩而编程 我使用Python 3.5.2测试了这个脚本: import time, telnetlib host = "dxc.ve7cc.net" port = 23 timeout = 9999 try: session = telnetlib.Telnet(host, port, timeout) except socket.timeout: print ("soc

我是Python的新手,但大约从1980年开始,我就一直在(Liberty-)Basic中为好玩而编程

我使用Python 3.5.2测试了这个脚本:

import time, telnetlib

host    = "dxc.ve7cc.net"
port    = 23
timeout = 9999

try:
    session = telnetlib.Telnet(host, port, timeout)
except socket.timeout:
    print ("socket timeout")
else:
    session.read_until("login: ")
    session.write("on0xxx\n")
    output = session.read_some()
    while output:
        print (output)
        time.sleep(0.1)  # let the buffer fill up a bit
        output = session.read_some()
有人能告诉我为什么会出现TypeError:像字节一样的对象是必需的,而不是'str',以及我如何解决它吗?

在Python3(但不是Python2)中,这是不能混合的。不能将
str
直接写入套接字;您必须使用
字节
。只需在字符串文本前面加上
b
,使其成为
bytes
文本即可

session.write(b"on0xxx\n")

与Python2.x不同,Python2.x不需要对通过网络发送的数据进行编码,而必须使用Python3.x。
因此,您要发送的所有内容都需要使用.encode()函数进行编码。您收到的所有内容都需要使用.decode()进行解码。

这可能是我需要的,谢谢!我还必须在这里添加“b”:session.write(b“on0xxx\n”)