struct.error: repeat count given without format specifier
struct.error: repeat count given without format specifier
详情
我正在尝试了解实现选择性重复 (SR) 协议的网络程序。过程如下:
- 运行 server.py 通过指定端口号、协议和 window 大小
- 运行 client.py 通过指定文件名、端口号和数据包数
当我 运行 client.py, 我收到这个错误:
Traceback (most recent call last):
File "client.py", line 238, in <module>
cs = pack('IHH' + str(len(data)), 's', seqNum, header, data)
struct.error: repeat count given without format specifier
这是让我感到困惑的代码块:
while not sendComplete:
toSend = lastInWindow + 1
data = GetMessage()
header = int('0101010101010101', 2)
cs = pack('IHH' + str(len(data)), 's', seqNum, header, data)
checksum = CalculateChecksum(cs)
packet = pack('IHH' + str(len(data)) + 's', seqNum, checksum, header, data)
if toSend < windowSize:
sendBuffer.append(packet)
timeoutTimers.append(TIMEOUT)
else:
sendBuffer[toSend % windowSize] = packet
timeoutTimers[toSend % windowSize] = TIMEOUT
疑难解答
我已经采取了几个步骤来解决这个问题,但所有这些都失败了(很明显),我不确定我是在接近光明还是在深入树林。
- 按照 PyCharm
的建议使用 struct.pack(fmt=...)
- 根据 PyCharm 的建议,我使用 +
连接参数
- 我再次 运行 程序并收到 TypeError: must be str, not int 错误,我试图通过实施 str 来修复(seqNum), str(header)等
- 最后,我得到一个 TypeError: pack() takes no keyword arguments 响应并认输。
如果有人能解释这里发生了什么,以及我如何启动这个程序并 运行ning,我将不胜感激。
问题是您在 pack
格式函数上打错了,而不是 (',' 而不是 '+'):
pack('IHH' + str(len(data))<strong>,</strong> 's', seqNum, header, data)
应该是:
pack('IHH' + str(len(data)) <strong>+</strong> 's', seqNum, header, data)
发生这种情况是因为每次字节格式上有一个数字时,它都会重复下一个字符数次。所以一个4h
会变成hhhh
。错误的行以数字结尾(例如:"IHH6"),所以它不知道要重复什么。
此外,您正在打包 4 "things",您应该向打包函数添加另一个参数:
pack('IHH' + str(len(data)) + 's', seqNum, header, data, missing_var)
详情
我正在尝试了解实现选择性重复 (SR) 协议的网络程序。过程如下:
- 运行 server.py 通过指定端口号、协议和 window 大小
- 运行 client.py 通过指定文件名、端口号和数据包数
当我 运行 client.py, 我收到这个错误:
Traceback (most recent call last):
File "client.py", line 238, in <module>
cs = pack('IHH' + str(len(data)), 's', seqNum, header, data)
struct.error: repeat count given without format specifier
这是让我感到困惑的代码块:
while not sendComplete:
toSend = lastInWindow + 1
data = GetMessage()
header = int('0101010101010101', 2)
cs = pack('IHH' + str(len(data)), 's', seqNum, header, data)
checksum = CalculateChecksum(cs)
packet = pack('IHH' + str(len(data)) + 's', seqNum, checksum, header, data)
if toSend < windowSize:
sendBuffer.append(packet)
timeoutTimers.append(TIMEOUT)
else:
sendBuffer[toSend % windowSize] = packet
timeoutTimers[toSend % windowSize] = TIMEOUT
疑难解答
我已经采取了几个步骤来解决这个问题,但所有这些都失败了(很明显),我不确定我是在接近光明还是在深入树林。
- 按照 PyCharm 的建议使用 struct.pack(fmt=...)
- 根据 PyCharm 的建议,我使用 + 连接参数
- 我再次 运行 程序并收到 TypeError: must be str, not int 错误,我试图通过实施 str 来修复(seqNum), str(header)等
- 最后,我得到一个 TypeError: pack() takes no keyword arguments 响应并认输。
如果有人能解释这里发生了什么,以及我如何启动这个程序并 运行ning,我将不胜感激。
问题是您在 pack
格式函数上打错了,而不是 (',' 而不是 '+'):
pack('IHH' + str(len(data))<strong>,</strong> 's', seqNum, header, data)
应该是:
pack('IHH' + str(len(data)) <strong>+</strong> 's', seqNum, header, data)
发生这种情况是因为每次字节格式上有一个数字时,它都会重复下一个字符数次。所以一个4h
会变成hhhh
。错误的行以数字结尾(例如:"IHH6"),所以它不知道要重复什么。
此外,您正在打包 4 "things",您应该向打包函数添加另一个参数:
pack('IHH' + str(len(data)) + 's', seqNum, header, data, missing_var)