TypeError: a bytes-like object is required, not 'str' when I'm inputting command in console
TypeError: a bytes-like object is required, not 'str' when I'm inputting command in console
我正在尝试使用 python 中的套接字模块连接到服务器。但是,我收到如下所示的错误消息。
TypeError: a bytes-like object is required, not 'str'
这是我的代码:
import socket
HOST = '0.0.0.0'
PORT = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")
while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command)
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data + "\n")
client_socket.close()
是什么导致了这个TypeError?我应该怎么做才能解决这个问题?
我也乐于接受任何改进我的 python 套接字代码编写的建议。
很简单,只需将字符串转换为 byte-wise 对象
string = "example string"
byte_wise_string = string.encode("utf-8")
希望能帮到你
问候
https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket.send
你想要
此错误是因为 python 期望通过套接字发送的是字节而不是字符串。
因此,您必须在发送之前将字符串转换为字节。您可以使用 encode()
将字符串转换为字节,并使用 decode()
将收到的字节转换为字符串。
我已经更新了你的代码,请参考下面,这应该可以正常工作。
import socket
HOST = '0.0.0.0'
PORT = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")
while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command.encode('utf-8')) #Encoding required here
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data.decode('utf-8') + "\n") #Decoding required here
client_socket.close()
我正在尝试使用 python 中的套接字模块连接到服务器。但是,我收到如下所示的错误消息。
TypeError: a bytes-like object is required, not 'str'
这是我的代码:
import socket
HOST = '0.0.0.0'
PORT = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")
while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command)
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data + "\n")
client_socket.close()
是什么导致了这个TypeError?我应该怎么做才能解决这个问题? 我也乐于接受任何改进我的 python 套接字代码编写的建议。
很简单,只需将字符串转换为 byte-wise 对象
string = "example string"
byte_wise_string = string.encode("utf-8")
希望能帮到你
问候
https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket.send
你想要
此错误是因为 python 期望通过套接字发送的是字节而不是字符串。
因此,您必须在发送之前将字符串转换为字节。您可以使用 encode()
将字符串转换为字节,并使用 decode()
将收到的字节转换为字符串。
我已经更新了你的代码,请参考下面,这应该可以正常工作。
import socket
HOST = '0.0.0.0'
PORT = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")
while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command.encode('utf-8')) #Encoding required here
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data.decode('utf-8') + "\n") #Decoding required here
client_socket.close()