def encode_multipart_formdata(self, fields, files):
"""
Properly encodes the multipart body of the request
:param fields: a dict, the parameters used in the request
:param files: a list of tuples containing information about the files
:returns: the content for the body and the content-type value
"""
import mimetools
import mimetypes
BOUNDARY = mimetools.choose_boundary()
CRLF = '\r\n'
L = []
for (key, value) in fields.items():
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="{0}"'.format(key))
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(key, filename))
L.append('Content-Type: {0}'.format(mimetypes.guess_type(filename)[0] or 'application/octet-stream'))
L.append('Content-Transfer-Encoding: binary')
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary={0}'.format(BOUNDARY)
return content_type, body
评论列表
文章目录