我正在尝试编写具有多个输入的字符串,但该字符串应该转换为字节,因为我正在编写一个连接到服务器(使用套接字)的程序,该程序使用需要以字节接收消息的协议。我正在执行以下代码:
print("Please introduce your username: ")
username = input()
print("Please introduce your password: ")
password = input()
client_socket.send(b"AUTH:%s:%s\n"%(username, password))
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
%b requires a bytes-like object, or an object that implements ____bytes____, not 'str'
Run Code Online (Sandbox Code Playgroud)
服务器应该收到以下消息:
AUTH:username:password
Run Code Online (Sandbox Code Playgroud)
使用适当的用户名和密码才能登录。
您对如何进行这项工作有任何想法吗?