code: def encode_header(cls, fin, opcode, mask, length, flags):
          
          first_byte = opcode
          second_byte = 0
          if six.PY2:
              extra = ''
          else:
              extra = b''
          if fin:
              first_byte |= cls.FIN_MASK
          if flags & cls.RSV0_MASK:
              first_byte |= cls.RSV0_MASK
          if flags & cls.RSV1_MASK:
              first_byte |= cls.RSV1_MASK
          if flags & cls.RSV2_MASK:
              first_byte |= cls.RSV2_MASK
          # now deal with length complexities
          if length < 126:
              second_byte += length
          elif length <= 0xffff:
              second_byte += 126
              extra = struct.pack('!H', length)
          elif length <= 0xffffffffffffffff:
              second_byte += 127
              extra = struct.pack('!Q', length)
          else:
              raise FrameTooLargeException
          if mask:
              second_byte |= cls.MASK_MASK
              extra += mask
          if six.PY3:
              return bytes([first_byte, second_byte]) + extra
          return chr(first_byte) + chr(second_byte) + extra
docs: Encodes a WebSocket header.
  
          :param fin: Whether this is the final frame for this opcode.
          :param opcode: The opcode of the payload, see `OPCODE_*`
          :param mask: Whether the payload is masked.
          :param length: The length of the frame.
          :param flags: The RSV* flags.
          :return: A bytestring encoded header.