通过UDP发送文件分为多个片段 [英] Sending file over UDP divided into fragments
问题描述
我一直在处理发送文件,该文件分为多个片段,这些片段由用户在输入时设置.问题是,我遇到了错误:
I have been dealing with sending file which is divided into fragments set by user on input. Problem is, I am getting error:
rec_list[fragIndex - 1] = data
IndexError: list assignment index out of range
我也像在聊天中一样发送单字符串消息,它可以正常工作. 我找不到代码中的错误,但也许可以.
I am also sending single string messages like in a chat and it works normally. I can't find a bug in my code but maybe you can.
顺便说一句,有些变量可能对数学可能不适合的数学有所帮助.
Btw, there are variables which might help with math which doesn't fit probably.
fragSize = 3
fragIndex = 216
fragCount = 215
问题是,片段总数应为215(发送前已预先计算-可以),index
不应超过count
!那就是问题所在.字符串不会发生这种情况.仅在这里带文件.
Problem is, total number of fragments should be 215 (precalculated before sending - IS OK), index
shouldn't be more than count
! That is the problem. And it doesn't happen with strings. Only here with file.
发送:
fragSize = int(input('Fragment size: ')) #max size of fragment
while True:
message = input('Enter message: ')
fragIndex=0 #reset fragment indexing
#asking for fragment size
if(message[:3] == '-sf'):
fragSize = int(input('Fragment size: '))
这是文件发送功能:
if (message[:2] == '-f'):
mType = 3
if message.startswith('-f'):
message = message[3:]
file_name = message
f=open(file_name,"rb")
contents = f.read()
fragCount = math.ceil(len(contents) / fragSize)
while contents!= '':
data = bytearray()
data.extend(contents[:fragSize])
fragIndex += 1
crc = crc32(data)
header = struct.pack('!hIIII', mType, fragSize, fragIndex, fragCount, crc)
self.sock.sendto(header + bytearray(data), (self.host, self.port))
contents = contents[fragSize:]
接收:
while True:
received_chunks = 0
rec_list = []
while True:
data, addr = sock.recvfrom(65535)
header = data[:18]
data = data[18:]
(mType, fragSize, fragIndex, fragCount, crc) = struct.unpack('!hIIII', header)
print(
'\nTyp: ' + str(mType) +
'\nFragSize: ' + str(fragSize) +
'\nFragIndex: ' + str(fragIndex) +
'\nFragCount: ' + str(fragCount) +
'\nCRC: ' + str(crc)
)
if len(rec_list) < fragCount:
need_to_add = fragCount - len(rec_list)
rec_list.extend([''] * need_to_add) # empty list for messages of size fragCount
rec_list[fragIndex - 1] = data
received_chunks += 1
if received_chunks == fragCount:
break # This is where the second while loop ends
仅当我要接收文件类型的消息时:(因为它分为更多的消息类型)
This is only if I want to receive message of type file: (because it is divided into more message types)
if mType == 3:
content = b''.join(rec_list)
f = open('filename.py','wb')
f.write(content)
推荐答案
您尝试将将苹果与桔子进行比较.好吧,从bytes
到str
,但是维基百科对此没有任何说明.
You tried to compare apples to oranges. Well, bytes
to str
but wikipedia doesn't say anything about that.
while contents!='':
...
contents
是bytes
对象,而''
是str
对象.在python 3中,这两个条件永远不会相等.打开外壳,我们看到了
contents
is a bytes
object and ''
is a str
object. In python 3, those two things can never be equal. Firing up the shell we see that
>>> b''==''
False
>>>
>>> contents = b"I am the very model"
>>> while contents != '':
... if not contents:
... print("The while didn't catch it!")
... break
... contents = contents[3:]
...
The while didn't catch it!
由于所有对象都具有真实性(即bool(some_object)
是有意义的),并且bytes
对象在为空时会变成False
,因此您可以这样做
Since all objects have a truthiness (that is, bool(some_object)
is meaningful) and bytes
objects turn False
when they are empty, you can just do
while contents:
....
更新
这不是原始答案的一部分,但是提出了有关将重试发送回客户端的问题.服务器端已在此处绘制
Not part of the original answer but a question was raised about sending retries back to the client. The server side is sketched in here
while True:
received_chunks = 0
fragCount = -1
rec_list = []
while True:
# wait forever for next conversation
if fragCount == -1:
sock.settimeout(None)
try:
data, addr = sock.recvfrom(65535)
except socket.timeout:
# scan for blank slots in rec_list
retries = [i for i, data in rec_list if not data]
# packet is mType, numFrags, FragList
# TODO: I just invented 13 for retry mtype
sock.sendto(struct.pack("!{}h".format(len(retries+2)), 13,
len(retries), *retries)
continue
# our first packet, set timeout for retries
if fragCount == -1:
sock.settimeout(2)
header = data[:18]
data = data[18:]
(mType, fragSize, fragIndex, fragCount, crc) = struct.unpack('!hIIII', header)
print(
'\nTyp: ' + str(mType) +
'\nFragSize: ' + str(fragSize) +
'\nFragIndex: ' + str(fragIndex) +
'\nFragCount: ' + str(fragCount) +
'\nCRC: ' + str(crc)
)
if len(rec_list) < fragCount:
need_to_add = fragCount - len(rec_list)
rec_list.extend([''] * need_to_add) # empty list for messages of size fragCount
rec_list[fragIndex - 1] = data
received_chunks += 1
if received_chunks == fragCount:
break # This is where the second while loop ends
这篇关于通过UDP发送文件分为多个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!