gpt4 book ai didi

python - "AttributeError: ' 字节 ' object has no attribute ' 编码 ' "

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:07 24 4
gpt4 key购买 nike

我在尝试运行代码时收到以下错误。这是一个片段:

import time;
from socket import*
from pip._vendor.distlib.compat import raw_input

pingCount = 0
minTime = 0
maxTime = 0
counter = 0
totalTime = 0
message = 'test'
packetsLost = 0

#Sends 10 pingcounts as setup_testing_defaults
while pingCount < 11:
counter +=1

#Creates a UDP Socket
clientSocket = socket(AF_INET, SOCK_DGRAM)

#Sets timeout value for each one to 1 second
#The timeout function determines how long till it expires
clientSocket.settimeout(1)

#Creating the paramaters for sendTo
#SendTo sends the ping to the socket
clientSocket.sendto(message.encode("utf-8"),('127.0.0.1',12000))

#time() yields the current time in milliseconds
start = time.time()

#Trying to print data received from the server
try: #etc...

代码运行了几次迭代(通常最多 3 次,然后因上述错误而崩溃。我不太确定发生了什么,所以任何建议都会很棒,谢谢!

最佳答案

这可能是代码中稍后重新分配 message 的内容。到 bytes对象 - 也许您正在重新分配从 clientSocket 收到的数据?如果是这样,数据返回 clientSocketbytes对象并且需要是 decode d,类似于您使用 message.encode() 的方式通过客户端发送文本数据。

关于 bytes 的用法有一个很好的解释用于 IO 通信的对象 - 特别是如果您习惯了 python2.x 的处理方式 - here

关于python - "AttributeError: ' 字节 ' object has no attribute ' 编码 ' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com