docs: def generate_bytes(cls, payload, fin_bit, opcode, mask_payload):
          """ Format data to string (buffered_bytes) to send to server.
          """
          # the first byte contains the FIN bit, the 3 RSV bits and the
          # 4 opcode bits and for a client will *always* be 1000 0001 (or 129).
          # so we want the first byte to look like...
          #
          #  1 0 0 0 0 0 0 1   (1 is a text frame)
          # +-+-+-+-+-------+
          # |F|R|R|R| opcode|
          # |I|S|S|S|       |
          # |N|V|V|V|       |
          # | |1|2|3|       |
          # +-+-+-+-+-------+
          # note that because all RSV bits are zero, we can ignore them
  
          # this shifts each bit into position and bitwise ORs them together,
          # using the struct module to pack them as incoming network bytes
          frame = pack(
              '!B', (
                  (fin_bit << 7) | opcode
              )
          )
  
          # the second byte - and maybe the 7 after this, we'll use to tell
          # the server how long our payload is.
  
          #                 +-+-------------+-------------------------------+
          #                 |M| Payload len |    Extended payload length    |
          #                 |A|     (7)     |             (16/63)           |
          #                 |S|             |   (if payload len==126/127)   |
          #                 |K|             |                               |
          # +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
          # |     Extended payload length continued, if payload len == 127  |
          # + - - - - - - - - - - - - - - - +-------------------------------+
  
          # the mask is always included with client -> server, so the first bit
          # of the second byte is always 1 which flags that the data is masked,
          # i.e. encoded
          if mask_payload:
              mask_bit = 1 << 7
          else:
              mask_bit = 0 << 7
          # next we have to | this bit with the payload length.
          # note that we ensure that the payload is utf-8 encoded before we take
          # the length because unicode characters can be >1 bytes in length and
          # lead to bugs if we just do ``len(payload)``.
          length = len(payload.encode('utf-8'))
  
          if length >= Frame.MAX_LENGTH:
              raise WebsocktProtocolError("data is too long")
  
          # the second byte contains the payload length and mask
          if length < Frame.LENGTH_7:
              # we can simply represent payload length with first 7 bits
              frame += pack('!B', (mask_bit | length))
          elif length < Frame.LENGTH_16:
              frame += pack('!B', (mask_bit | 126)) + pack('!H', length)
          else:
              frame += pack('!B', (mask_bit | 127)) + pack('!Q', length)
  
          if mask_payload:
              # we always mask frames from the client to server
              # use a string of n random buffered_bytes for the mask
              mask_key = os.urandom(4)
              mask_data = cls.generate_mask(mask_key=mask_key, data=payload)
              mask = mask_key + mask_data
              frame += mask
          else:
              frame += bytearray(payload, 'utf-8')
  
          return bytearray(frame)