【发布时间】:2016-11-14 14:55:11
【问题描述】:
我在尝试运行我的代码时遇到以下错误。这是一个sn-p:
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 次,然后因上述错误而崩溃。我不太确定发生了什么,所以任何建议都会很棒,谢谢!
【问题讨论】:
-
在 while 循环的第二次或第三次迭代中,
message可能是bytes类型。当然,哪个没有编码方法。 -
这段代码对我有用。也许是尝试后的代码?
-
投票结束基于有问题的代码没有出现错误的报告。此外,您还有缩进问题;
while命令后面的代码需要另一个缩进级别才能实际包含在while循环中。如果不修改,你的代码会在那里抛出语法错误。 -
需要注意的一些事情:您的 raw_input 似乎错误(即:从 pip 导入?)...如果您使用的是 Python 3,则应该使用 input 代替。另外,请包括实际的堆栈跟踪,仅您的示例部分代码不足以诊断它。
标签: python python-3.x