code: def pack(self):
          
          payload = io.BytesIO()
          # Advance num bytes equal to header size - the header is written later
          # after the payload of all segments and parts has been written:
          payload.seek(self.header_size, io.SEEK_CUR)
  
          # Write out payload of segments and parts:
          self.build_payload(payload)
  
          packet_length = len(payload.getvalue()) - self.header_size
          self.header = MessageHeader(self.session_id, self.packet_count, packet_length, constants.MAX_SEGMENT_SIZE,
                                      num_segments=len(self.segments), packet_options=0)
          packed_header = self.header_struct.pack(*self.header)
  
          # Go back to begining of payload for writing message header:
          payload.seek(0)
          payload.write(packed_header)
          payload.seek(0, io.SEEK_END)
  
          trace(self)
  
          return payload
docs: Pack message to binary stream.