'Encode fields and files for posting to an HTTP server.
:param fields: The fields to send as a list of (fieldname, value)
tuples.
:param files: The files to send as a list of (fieldname, filename,
file_bytes) tuple.'

def encode_request(self, fields, files):

    parts = []
    boundary = self.boundary
    for (k, values) in fields:
        if (not isinstance(values, (list, tuple))):
            values = [values]
        for v in values:
            parts.extend((('--' + boundary), ('Content-Disposition:    form-data;    name="%s"' % k).encode('utf-8'), '', v.encode('utf-8')))
    for (key, filename, value) in files:
        parts.extend((('--' + boundary), ('Content-Disposition:    form-data;    name="%s";    filename="%s"' % (key, filename)).encode('utf-8'), '', value))
    parts.extend(((('--' + boundary) + '--'), ''))
    body = '\r\n'.join(parts)
    ct = ('multipart/form-data;    boundary=' + boundary)
    headers = {'Content-type': ct, 'Content-length': str(len(body))}
    return Request(self.url, body, headers)
