简体   繁体   English

python 套接字编程类型错误:不需要像 object 这样的字节 str

[英]python socket programming TypeError: bytes like object is required not str

I have a Python server running that listen to data that is sent by the Python client.我有一个 Python 服务器正在运行,它监听 Python 客户端发送的数据。 The client takes input from the user and sends it to the server which prints it.客户端从用户那里获取输入并将其发送到打印它的服务器。 However, I get the error that says "TyperError: a byte-like object is required, not 'str'".但是,我收到错误消息“TyperError:需要类似字节的 object,而不是 'str'”。 It is on line number 8 of the client code.它位于客户端代码的第 8 行。

SERVER CODE:服务器代码:

import socket

def server(interface, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((interface, port))
    sock.listen(1)
    print('Listening at', sock.getsockname())
    while True:
        sc, sockname = sock.accept()
        print('We have accepted a connection from', sockname)
        print(' Socket name:', sc.getsockname())
        print(' Socket peer:', sc.getpeername())
        message = sc.recv(1024)
        print(' Incoming sixteen-octet message:', repr(message))
        sc.sendall(b'Farewell, client')
        sc.close()
        print(' Reply sent, socket closed')

if __name__ == '__main__':
    server('0.0.0.0', 9999)

CLIENT CODE:客户代码:

import socket

def client(host, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    print('Client has been assigned socket name', sock.getsockname())
    command = input("message > ")
    sock.sendall(command)
    reply = sock.recv(1024)
    print('The server said', repr(reply))
    sock.close()

if __name__ == '__main__':
    client('localhost', 9999)

TyperError: a byte-like object is required, not 'str' TyperError:需要类似字节的 object,而不是“str”

socket.sendall expects bytes , but you have passed in the result of input("message > ") , which is a str . socket.sendall需要bytes ,但是您已经传入了input("message > ")的结果,这是一个str So, you need to somehow convert the str from the input into bytes .因此,您需要以某种方式将strinput转换为bytes You can use encode to do this:您可以使用encode来执行此操作:

command = input("message > ").encode()

encode , well, encodes a str into bytes . encode ,嗯,将str编码bytes

While communicating in Python, you should encode the parameter you send as a byte and decode the parameter you received from the byte format.在 Python 中进行通信时,您应该将发送的参数编码为字节,并将接收到的参数解码为字节格式。 You can understand what I want to say from the code below.你可以从下面的代码中理解我想说的话。

Server Code:服务器代码:

import socket

def server(interface, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((interface, port))
    sock.listen(1)
    print('Listening at', sock.getsockname())
    while True:
        sc, sockname = sock.accept()
        print('We have accepted a connection from', sockname)
        print(' Socket name:', sc.getsockname())
        print(' Socket peer:', sc.getpeername())
        message = sc.recv(1024).decode('utf-8')
        print(' Incoming sixteen-octet message:', repr(message))
        sc.sendall(bytes('Farewell, client','utf-8'))
        sc.close()
        print(' Reply sent, socket closed')

if __name__ == '__main__':
    server('0.0.0.0', 9999)

Client Code:客户代码:

import socket

def client(host, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    print('Client has been assigned socket name', sock.getsockname())
    command = input("message > ")
    sock.sendall(bytes(command,'utf-8'))
    reply = sock.recv(1024).decode('utf-8)
    print('The server said', repr(reply))
    sock.close()

if __name__ == '__main__':
    client('localhost', 9999)

If you edit your code this way, I think you will correct the error you received.如果您以这种方式编辑代码,我认为您将更正收到的错误。 I hope it helps as I am new to this.我希望它有所帮助,因为我是新手。 Sorry for my entry-level English.对不起我的入门级英语。

暂无
暂无

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

相关问题 套接字编程:类型错误:需要类似字节的 object,而不是“str” - Socket programming: TypeError: a bytes-like object is required, not 'str' 使用套接字代码并将其更改为python 3.5.0并获取TypeError:需要一个类似字节的对象,而不是'str'错误 - Using socket code and changing it to python 3.5.0 and getting TypeError: a bytes-like object is required, not 'str' error TypeError:需要一个类似字节的对象,而不是'str'python - TypeError: a bytes-like object is required, not 'str' python TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str” - TypeError: a bytes-like object is required, not 'str' for Image command in Python Python TypeError:需要一个类似字节的对象,而不是'str' - Python TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是'str'python3 - TypeError: a bytes-like object is required, not 'str' python3 类型错误:需要类似字节的 object,而不是使用子进程 python 的“str” - TypeError: a bytes-like object is required, not 'str' using Subprocess python Python 错误类型错误:需要类似字节的 object,而不是“str” - Python ERROR TypeError: a bytes-like object is required, not 'str' python错误TypeError:需要一个类似字节的对象,而不是'str' - python error TypeError: a bytes-like object is required,not 'str' 类型错误:需要一个类似字节的对象,而不是 python 和 CSV 中的“str” - TypeError: a bytes-like object is required, not 'str' in python and CSV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM