def encode_multipart(fields, files, boundary=None):
    """Prepare a multipart HTTP request.

    *fields* is a sequence of (name: str, value: str) elements for regular
    form fields, *files* is a sequence of (name: str, filename: str, value:
    bytes) elements for data to be uploaded as files.

    Returns (content_type: bytes, body: bytes) ready for http.client.HTTP.
    """
    if boundary is None:
        boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
    elif not isinstance(boundary, bytes):
        raise TypeError('boundary must be bytes, not %r' % type(boundary))
    l = []
    for key, values in fields:
        if not isinstance(values, (tuple, list)):
            values = [values]
        for value in values:
            l.extend((b'--' + boundary, (
                'Content-Disposition: form-data; name="%s"' % key).encode(
                'utf-8'), b'', value.encode('utf-8')))
    for key, filename, value in files:
        l.extend((b'--' + boundary, (
            'Content-Disposition: form-data; name="%s"; filename="%s"' % (
            key, filename)).encode('utf-8'), b'', value))
    l.append(b'--' + boundary + b'--')
    l.append(b'')
    body = b'\r\n'.join(l)
    content_type = b'multipart/form-data; boundary=' + boundary
    return content_type, body
