[英]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
.因此,您需要以某种方式将
str
从input
转换为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.