'Fields are a dict of form name-> value
For files, value should be a file object.
Other file-like objects might work and a fake name will be chosen.
Return (content_type, body) ready for httplib.HTTP instance'

def _encode_multipart_form(self, fields):

    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields.items():
        logging.debug(('Encoding    %s,    (%s)%s' % (key, type(value), value)))
        if (not value):
            continue
        L.append(('--' + BOUNDARY))
        if (hasattr(value, 'read') and callable(value.read)):
            filename = getattr(value, 'name', ('%s.jpg' % key))
            L.append(('Content-Disposition:    form-data;    name="%s";    filename="%s"' % (key, filename)))
            L.append('Content-Type:    image/jpeg')
            value = value.read()
            logging.debug(type(value))
        else:
            L.append(('Content-Disposition:    form-data;    name="%s"' % key))
        L.append('')
        if isinstance(value, unicode):
            logging.debug('Convert    to    ascii')
            value = value.encode('ascii')
        L.append(value)
    L.append((('--' + BOUNDARY) + '--'))
    L.append('')
    body = CRLF.join(L)
    content_type = ('multipart/form-data;    boundary=%s' % BOUNDARY)
    return (content_type, body)
