[英]typeError: a byte-like object is required, not 'str' in Python 3
I am new to python and I am experimenting things.我是 python 的新手,我正在尝试一些东西。 I am trying to solve this UDP client and server program in python3 using python socket.
我正在尝试使用 python 套接字在 python3 中解决这个 UDP 客户端和服务器程序。
The objective of my UDP server program named UDpserver.py are as follows:我的名为 UDpserver.py 的 UDP 服务器程序的目标如下:
The objective of a client program named UDpclient.py that connects to above server and receives a response from the server:名为 UDpclient.py 的客户端程序的目标连接到上述服务器并接收来自服务器的响应:
following is the server program's code (UDpserver.py):以下是服务器程序的代码(UDpserver.py):
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
Following is the client program code(UDpclient.py)以下是客户端程序代码(UDpclient.py)
import socket
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
When I run the client program(Udpclient.py) I get this error after it lets me input lowercase letters:当我运行客户端程序(Udpclient.py)时,它让我输入小写字母后出现此错误:
TypeError: a bytes-like object is required, not 'str' for line 7 ie clientSocket.sendto(message,(serverName, serverPort))类型错误:需要一个类似字节的对象,而不是第 7 行的“str” ,即 clientSocket.sendto(message,(serverName, serverPort))
TypeError: a bytes-like object is required, not 'str'类型错误:需要类似字节的对象,而不是“str”
If anybody could please help me solve this.如果有人可以请帮我解决这个问题。 Thank you.
谢谢你。
You can use message.encode()
to encode the string type message.您可以使用
message.encode()
对字符串类型消息进行编码。 So this is how your UDPclient.py
would look like:所以这就是你的
UDPclient.py
样子:
import socket
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = input(’Input lowercase sentence:’)
clientSocket.sendto(message.encode(),(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
Python 3 strings fully support the unicode specification. Python 3 字符串完全支持 unicode 规范。 An in-memory string must be serialized in some way to conform to some sort of encoding such as ASCII, UTF-8, various Windows code pages and etc, before being sent on the network.
在网络上发送之前,内存中的字符串必须以某种方式序列化以符合某种编码,例如 ASCII、UTF-8、各种 Windows 代码页等。
sendto
expects an already-encoded bytes string. sendto
需要一个已经编码的字节字符串。
Since you are transfering data between machines, its not a good idea to let each one choose its own encoding.由于您在机器之间传输数据,让每个机器选择自己的编码并不是一个好主意。 The easiest option for a home grown protocol is to just declare that you will use UTF-8, which is the most interoperable unicode encoding.
自主开发协议的最简单选项是声明您将使用 UTF-8,这是最具互操作性的 unicode 编码。
clientSocket.sendto(message.encode("utf-8"),(serverName, serverPort))
The receiver would decode using the same encoding.接收器将使用相同的编码进行解码。
将“主机名”更改为实际 IP 并使用以下命令更新错误代码后解决了该问题
clientSocket.sendto(message.encode(),(serverName, serverPort))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.