from __future__ import absolute_import import binascii import codecs import os from io import BytesIO from .packages import six from .packages.six import b from .fields import RequestField writer = codecs.lookup("utf-8")[3] def choose_boundary(): """ Our embarrassingly-simple replacement for mimetools.choose_boundary. """ boundary = binascii.hexlify(os.urandom(16)) if not six.PY2: boundary = boundary.decode("ascii") return boundary def iter_field_objects(fields): """ Iterate over fields. Supports list of (k, v) tuples and dicts, and lists of :class:`~urllib3.fields.RequestField`. """ if isinstance(fields, dict): i = six.iteritems(fields) else: i = iter(fields) for field in i: if isinstance(field, RequestField): yield field else: yield RequestField.from_tuples(*field) def iter_fields(fields): """ .. deprecated:: 1.6 Iterate over fields. The addition of :class:`~urllib3.fields.RequestField` makes this function obsolete. Instead, use :func:`iter_field_objects`, which returns :class:`~urllib3.fields.RequestField` objects. Supports list of (k, v) tuples and dicts. """ if isinstance(fields, dict): return ((k, v) for k, v in six.iteritems(fields)) return ((k, v) for k, v in fields) def encode_multipart_formdata(fields, boundary=None): """ Encode a dictionary of ``fields`` using the multipart/form-data MIME format. :param fields: Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestField`). :param boundary: If not specified, then a random boundary will be generated using :func:`urllib3.filepost.choose_boundary`. """ body = BytesIO() if boundary is None: boundary = choose_boundary() for field in iter_field_objects(fields): body.write(b("--%s\r\n" % (boundary))) writer(body).write(field.render_headers()) data = field.data if isinstance(data, int): data = str(data) # Backwards compatibility if isinstance(data, six.text_type): writer(body).write(data) else: body.write(data) body.write(b"\r\n") body.write(b("--%s--\r\n" % (boundary))) content_type = str("multipart/form-data; boundary=%s" % boundary) return body.getvalue(), content_type
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
contrib | Folder | 0755 |
|
|
packages | Folder | 0755 |
|
|
util | Folder | 0755 |
|
|
.__init__.pyo.40009 | File | 2.84 KB | 0644 |
|
._collections.pyo.40009 | File | 13.53 KB | 0644 |
|
.connection.pyo.40009 | File | 12.35 KB | 0644 |
|
.connectionpool.pyo.40009 | File | 27.27 KB | 0644 |
|
.exceptions.pyo.40009 | File | 14.46 KB | 0644 |
|
.fields.pyo.40009 | File | 9.34 KB | 0644 |
|
.filepost.pyo.40009 | File | 3.46 KB | 0644 |
|
.poolmanager.pyo.40009 | File | 15.94 KB | 0644 |
|
.request.pyo.40009 | File | 6.07 KB | 0644 |
|
.response.pyo.40009 | File | 24.91 KB | 0644 |
|
__init__.py | File | 2.62 KB | 0644 |
|
__init__.pyc | File | 2.84 KB | 0644 |
|
__init__.pyo | File | 2.84 KB | 0644 |
|
_collections.py | File | 10.54 KB | 0644 |
|
_collections.pyc | File | 13.53 KB | 0644 |
|
_collections.pyo | File | 13.53 KB | 0644 |
|
connection.py | File | 14.12 KB | 0644 |
|
connection.pyc | File | 12.35 KB | 0644 |
|
connection.pyo | File | 12.35 KB | 0644 |
|
connectionpool.py | File | 34.89 KB | 0644 |
|
connectionpool.pyc | File | 27.27 KB | 0644 |
|
connectionpool.pyo | File | 27.27 KB | 0644 |
|
exceptions.py | File | 7 KB | 0644 |
|
exceptions.pyc | File | 14.46 KB | 0644 |
|
exceptions.pyo | File | 14.46 KB | 0644 |
|
fields.py | File | 8.35 KB | 0644 |
|
fields.pyc | File | 9.34 KB | 0644 |
|
fields.pyo | File | 9.34 KB | 0644 |
|
filepost.py | File | 2.38 KB | 0644 |
|
filepost.pyc | File | 3.46 KB | 0644 |
|
filepost.pyo | File | 3.46 KB | 0644 |
|
poolmanager.py | File | 17.44 KB | 0644 |
|
poolmanager.pyc | File | 15.94 KB | 0644 |
|
poolmanager.pyo | File | 15.94 KB | 0644 |
|
request.py | File | 5.88 KB | 0644 |
|
request.pyc | File | 6.07 KB | 0644 |
|
request.pyo | File | 6.07 KB | 0644 |
|
response.py | File | 27.56 KB | 0644 |
|
response.pyc | File | 24.91 KB | 0644 |
|
response.pyo | File | 24.91 KB | 0644 |
|