def encode(cls, v):
        """Return the given header name or value, encoded for HTTP output."""
        for enc in cls.encodings:
            try:
                return v.encode(enc)
            except UnicodeEncodeError:
                continue

        if cls.protocol == (1, 1) and cls.use_rfc_2047:
            # Encode RFC-2047 TEXT
            # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?=").
            # We do our own here instead of using the email module
            # because we never want to fold lines--folding has
            # been deprecated by the HTTP working group.
            v = b2a_base64(v.encode('utf-8'))
            return b'=?utf-8?b?' + v.strip(b'\n') + b'?='

        raise ValueError(
            'Could not encode header part %r using '
            'any of the encodings %r.' % (v, cls.encodings),
        )
Return the given header name or value, encoded for HTTP output.
cherrypy/lib/httputil.py