简体   繁体   English

typeError:需要一个类似字节的对象,而不是 Python 3 中的“str”

[英]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 服务器程序的目标如下:

  • Receives a message from a client接收来自客户端的消息
  • Responds to the client with the same message but all in UPPER CASE.使用相同的消息响应客户端,但全部为大写。

The objective of a client program named UDpclient.py that connects to above server and receives a response from the server:名为 UDpclient.py 的客户端程序的目标连接到上述服务器并接收来自服务器的响应:

  • sends a message to the server向服务器发送消息
  • Display the message received from the server.显示从服务器收到的消息。 The displayed message should be in upper case.显示的消息应为大写。

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.

相关问题 类型错误:需要类似字节的 object,而不是“str” - TypeError : a byte-like object is required , not 'str' Python 3 移植问题,说需要一个类似字节的对象? - Python 3 porting issue, says a byte-like object is required? 使用 Python 3 A byte-like object is required not a str" 我尝试将其转换为字节,但它仍然给我同样的错误 - using Python 3 A byte-like object is required not a str" i have tried converting it into bytes but it still giving me the same error 类型错误:类似字节的 object,不是字符串 - TypeError: Byte-like object, not string 将字符串放在 python 中的字节状 object - Put string on byte-like object in python 如何在Python 3中将二进制字符串转换为类似字节的对象? - How to Convert Binary String to Byte-like Object in Python 3? 如何制作 Python requests.Response object 字节状? - How to make a Python requests.Response object byte-like? 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 TypeError:需要一个类似字节的对象,而不是python 3.5中的“ str” - TypeError: a bytes-like object is required, not 'str' in python 3.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM