diff options
Diffstat (limited to 'requests/packages/urllib3/filepost.py')
-rw-r--r-- | requests/packages/urllib3/filepost.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/requests/packages/urllib3/filepost.py b/requests/packages/urllib3/filepost.py index 2ffea8b..e1ec8af 100644 --- a/requests/packages/urllib3/filepost.py +++ b/requests/packages/urllib3/filepost.py @@ -1,18 +1,21 @@ # urllib3/filepost.py -# Copyright 2008-2011 Andrey Petrov and contributors (see CONTRIBUTORS.txt) +# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt) # # This module is part of urllib3 and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php import codecs -import mimetools import mimetypes try: - from cStringIO import StringIO + from mimetools import choose_boundary except ImportError: - from StringIO import StringIO # pylint: disable-msg=W0404 + from .packages.mimetools_choose_boundary import choose_boundary +from io import BytesIO + +from .packages import six +from .packages.six import b writer = codecs.lookup('utf-8')[3] @@ -35,37 +38,37 @@ def encode_multipart_formdata(fields, boundary=None): If not specified, then a random boundary will be generated using :func:`mimetools.choose_boundary`. """ - body = StringIO() + body = BytesIO() if boundary is None: - boundary = mimetools.choose_boundary() + boundary = choose_boundary() - for fieldname, value in fields.iteritems(): - body.write('--%s\r\n' % (boundary)) + for fieldname, value in six.iteritems(fields): + body.write(b('--%s\r\n' % (boundary))) if isinstance(value, tuple): filename, data = value writer(body).write('Content-Disposition: form-data; name="%s"; ' 'filename="%s"\r\n' % (fieldname, filename)) - body.write('Content-Type: %s\r\n\r\n' % - (get_content_type(filename))) + body.write(b('Content-Type: %s\r\n\r\n' % + (get_content_type(filename)))) else: data = value writer(body).write('Content-Disposition: form-data; name="%s"\r\n' % (fieldname)) - body.write('Content-Type: text/plain\r\n\r\n') + body.write(b'Content-Type: text/plain\r\n\r\n') if isinstance(data, int): data = str(data) # Backwards compatibility - if isinstance(data, unicode): + if isinstance(data, six.text_type): writer(body).write(data) else: body.write(data) - body.write('\r\n') + body.write(b'\r\n') - body.write('--%s--\r\n' % (boundary)) + body.write(b('--%s--\r\n' % (boundary))) - content_type = 'multipart/form-data; boundary=%s' % boundary + content_type = b('multipart/form-data; boundary=%s' % boundary) return body.getvalue(), content_type |