Spade
Mini Shell
| Directory:~$ /lib/python2.7/site-packages/requests_toolbelt/ |
| [Home] [System Details] [Kill Me] |
�
ӵ Yc@s\dZddlmZddlmZmZdefd��YZdefd��YZd S(
s
requests_toolbelt.streaming_iterator
====================================
This holds the implementation details for the :class:`StreamingIterator`.
It
is designed for the case where you, the user, know the size of the upload
but
need to provide the data as an iterator. This class will allow you to
specify
the size and stream the data without using a chunked transfer-encoding.
i����(t super_leni(t
CustomBytesIOtencode_withtStreamingIteratorcBs&eZdZdd�Zdd�ZRS(s
This class provides a way of allowing iterators with a known size to be
streamed instead of chunked.
In requests, if you pass in an iterator it assumes you want to use
chunked transfer-encoding to upload the data, which not all servers
support well. Additionally, you may want to set the content-length
yourself to avoid this but that will not work. The only way to preempt
requests using a chunked transfer-encoding and forcing it to stream the
uploads is to mimic a very specific interace. Instead of having to know
these details you can instead just use this class. You simply provide
the
size and iterator and pass the instance of StreamingIterator to
requests
via the data parameter like so:
.. code-block:: python
from requests_toolbelt import StreamingIterator
import requests
# Let iterator be some generator that you already have and size be
# the size of the data produced by the iterator
r = requests.post(url, data=StreamingIterator(size, iterator))
You can also pass file-like objects to :py:class:`StreamingIterator` in
case requests can't determize the filesize itself. This is the
case with
streaming file objects like ``stdin`` or any sockets. Wrapping e.g.
files
that are on disk with ``StreamingIterator`` is unnecessary, because
requests can determine the filesize itself.
Naturally, you should also set the `Content-Type` of your upload
appropriately because the toolbelt will not attempt to guess that for
you.
sutf-8cCs|t|�|_|jdkr-td��n|j|_||_||_t|d�rf||_nt||�|_dS(Nis1The
size of the upload must be a positive integertread( tinttsizet
ValueErrortlentencodingtiteratorthasattrt_filet_IteratorAsBinaryFile(tselfRR
R ((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyt__init__7s i����cCst|jj|�|j�S(N(RRRR (RR((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyROs(t__name__t
__module__t__doc__RR(((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyRs#R
cBs2eZdd�Zd�Zd�Zdd�ZRS(sutf-8cCs"||_||_t�|_dS(N(R
R Rt_buffer(RR
R ((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyRTs cCs6ytt|j�|j�SWntk
r1dSXdS(Nt(RtnextR
R t
StopIteration(R((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyt
_get_bytes_s
cCse|jj�|t|j�}t}x8|dkr`|r`|j�}||jj|�8}q)WdS(Ni(Rtsmart_truncateRtTrueRtappend(RRtamount_to_loadtbytes_to_append((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyt_load_byteses
i����cCsEt|�}|dkr(dj|j�S|j|�|jj|�S(Ni����R(RtjoinR
RRR(RR((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyRns
(RRRRRR(((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyR
Ss N( Rtrequests.utilsRtmultipart.encoderRRtobjectRR
(((sH/usr/lib/python2.7/site-packages/requests_toolbelt/streaming_iterator.pyt<module>sA