Commit 2661890a authored by Tanner Prestegard's avatar Tanner Prestegard

More compatibility fixes for Python 3.4

parent 453d760d
......@@ -2199,14 +2199,15 @@ def encode_multipart_formdata(fields, files):
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$'
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = b'\r\n'
L = []
for (key, value) in fields:
if value is None: continue
L.append(b'--' + BOUNDARY)
L.append(b'Content-Disposition: form-data; name="%s"' % key.encode())
L.append(b'')
L.append(('--' + BOUNDARY).encode())
L.append('Content-Disposition: form-data; name="{0}"'
.format(key).encode())
L.append(''.encode())
# encode in case it is unicode
if isinstance(value, six.binary_type):
L.append(value)
......@@ -2216,19 +2217,22 @@ def encode_multipart_formdata(fields, files):
L.append(str(value).encode())
for (key, filename, value) in files:
if value is None: continue
L.append(b'--' + BOUNDARY)
L.append(('--' + BOUNDARY).encode())
# str(filename) in case it is unicode
L.append(b'Content-Disposition: form-data; name="%s"; filename="%s"' % (key.encode(), filename.encode()))
L.append(b'Content-Type: %s' % get_content_type(filename).encode())
L.append(b'')
L.append(('Content-Disposition: form-data; name="{0}"; '
'filename="{1}"').format(key, filename).encode())
L.append('Content-Type: {0}'.format(get_content_type(filename))
.encode())
L.append(''.encode())
if isinstance(value, six.text_type):
L.append(value.encode())
else:
L.append(value)
L.append(b'--' + BOUNDARY + b'--')
L.append(b'')
L.append(('--' + BOUNDARY + '--').encode())
L.append(''.encode())
body = CRLF.join(L)
content_type = b'multipart/form-data; boundary=%s' % BOUNDARY
content_type = 'multipart/form-data; boundary={0}'.format(BOUNDARY) \
.encode()
return content_type, body
def get_content_type(filename):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment