title: Sending file over UDP divided into fragments
body: <p>I have been dealing with sending file which is divided into fragments set by user on input. Problem is, I am getting error: </p>
  
  <p><code>rec_list[fragIndex - 1] = data</code>
  <code>IndexError: list assignment index out of range</code></p>
  
  <p>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.</p>
  
  <p>Btw, there are variables which might help with math which doesn't fit probably.</p>
  
  <p><code>fragSize = 3</code></p>
  
  <p><code>fragIndex = 216</code></p>
  
  <p><code>fragCount = 215</code></p>
  
  <p>Problem is, total number of fragments should be 215 (precalculated before sending - IS OK), <code>index</code> shouldn't be more than <code>count</code>! That is the problem. And it doesn't happen with strings. Only here with file.</p>
  
  <p>Sending:</p>
  
  <pre><code>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: '))
  </code></pre>
  
  <p>And here is sending function for files:</p>
  
  <pre><code>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:]
  </code></pre>
  
  <p>Receiving: </p>
  
  <pre><code>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) &lt; 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
  </code></pre>
  
  <p>This is only if I want to receive message of type file: (because it is divided into more message types)</p>
  
  <pre><code>if mType == 3:
       content = b''.join(rec_list)
       f = open('filename.py','wb')
       f.write(content)
  </code></pre>