我在运行代码时遇到以下错误。以下是一个片段:
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
接收到的数据重新分配给它?如果是,返回的数据clientSocket
是一个bytes
对象,需要是decode
d,类似于使用message.encode()
通过客户端发送文本数据的方式。有一个很好的解释说明了
bytes
对象在IO通信中的用法-特别是如果您习惯了python2.x的做事方式-here相关问题 更多 >
编程相关推荐