404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@52.14.110.186: ~ $
3

�P\$Q�@s�dZddlZddlZddlZddlmZddlZddlmZGdd�de	�Z
Gdd	�d	e�Zd
d�Z
Gdd
�d
e�Zdd�Zdd�Zdd�Zejdd��Zdd�Zdd�ZGdd�de�ZGdd�dej�ZGdd�de�ZGd d!�d!e�ZdS)"z�

requests_toolbelt.multipart.encoder
===================================

This holds all of the implementation details of the MultipartEncoder

�N)�uuid4�)�fieldsc@seZdZdZdS)�FileNotSupportedErrorzFile not supported error.N)�__name__�
__module__�__qualname__�__doc__�r
r
�/usr/lib/python3.6/encoder.pyrsrc@s�eZdZdZd%dd�Zedd��Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zedd��Zd d!�Zd'd#d$�ZdS)(�MultipartEncodera�

    The ``MultipartEncoder`` object is a generic interface to the engine that
    will create a ``multipart/form-data`` body for you.

    The basic usage is:

    .. code-block:: python

        import requests
        from requests_toolbelt import MultipartEncoder

        encoder = MultipartEncoder({'field': 'value',
                                    'other_field', 'other_value'})
        r = requests.post('https://httpbin.org/post', data=encoder,
                          headers={'Content-Type': encoder.content_type})

    If you do not need to take advantage of streaming the post body, you can
    also do:

    .. code-block:: python

        r = requests.post('https://httpbin.org/post',
                          data=encoder.to_string(),
                          headers={'Content-Type': encoder.content_type})

    If you want the encoder to use a specific order, you can use an
    OrderedDict or more simply, a list of tuples:

    .. code-block:: python

        encoder = MultipartEncoder([('field', 'value'),
                                    ('other_field', 'other_value')])

    .. versionchanged:: 0.4.0

    You can also provide tuples as part values as you would provide them to
    requests' ``files`` parameter.

    .. code-block:: python

        encoder = MultipartEncoder({
            'field': ('file_name', b'{"a": "b"}', 'application/json',
                      {'X-My-Header': 'my-value'})
        ])

    .. warning::

        This object will end up directly in :mod:`httplib`. Currently,
        :mod:`httplib` has a hard-coded read size of **8192 bytes**. This
        means that it will loop until the file has been read and your upload
        could take a while. This is **not** a bug in requests. A feature is
        being considered for this object to allow you, the user, to specify
        what size should be returned on a read. If you have opinions on this,
        please weigh in on `this issue`_.

    .. _this issue:
        https://github.com/requests/toolbelt/issues/75

    N�utf-8cCs�|p
t�j|_dj|j�|_||_djt|j|j�td|j�g�|_||_	d|_
g|_tg�|_
d|_d|_t|d�|_|j�|j�dS)Nz--{0}�z
F)�encoding)r�hex�boundary_value�format�boundaryr�join�encode_with�_encoded_boundaryr�finished�parts�iter�_iter_parts�
_current_part�_len�
CustomBytesIO�_buffer�_prepare_parts�_write_boundary)�selfrrrr
r
r�__init__Ws
zMultipartEncoder.__init__cCs|jp|j�S)aiLength of the multipart/form-data body.

        requests will first attempt to get the length of the body by calling
        ``len(body)`` and then by checking for the ``len`` attribute.

        On 32-bit systems, the ``__len__`` method cannot return anything
        larger than an integer (in C) can hold. If the total size of the body
        is even slightly larger than 4GB users will see an OverflowError. This
        manifested itself in `bug #80`_.

        As such, we now calculate the length lazily as a property.

        .. _bug #80:
            https://github.com/requests/toolbelt/issues/80
        )r�_calculate_length)r!r
r
r�len�szMultipartEncoder.lencCsdj|j�S)Nz<MultipartEncoder: {0!r}>)rr)r!r
r
r�__repr__�szMultipartEncoder.__repr__cs2t|j��t�fdd�|jD���d|_|jS)z�
        This uses the parts to calculate the length of the body.

        This returns the calculated length so __len__ can be lazy.
        c3s|]}�t|�dVqdS)�N)�	total_len)�.0�p)�boundary_lenr
r�	<genexpr>�sz5MultipartEncoder._calculate_length.<locals>.<genexpr>r&)r$r�sumrr)r!r
)r*rr#�s
"z"MultipartEncoder._calculate_lengthcCs|t|j�}|dkr|SdS)a�This calculates how many bytes need to be added to the buffer.

        When a consumer read's ``x`` from the buffer, there are two cases to
        satisfy:

            1. Enough data in the buffer to return the requested amount
            2. Not enough data

        This function uses the amount of unread bytes in the buffer and
        determines how much the Encoder has to load before it can return the
        requested amount of bytes.

        :param int read_size: the number of bytes the consumer requests
        :returns: int -- the number of bytes that must be loaded into the
            buffer before the read can be satisfied. This will be strictly
            non-negative
        r)r'r)r!Z	read_size�amountr
r
r�_calculate_load_amount�sz'MultipartEncoder._calculate_load_amountcCs�|jj�|jp|j�}x�|dks*|dkr�d}|r^|j�r^||jd�7}||j�7}|j�}|sv||j�7}d|_P||j	|j|�7}|dkr||8}qWdS)z0Load ``amount`` number of bytes into the buffer.�rs
TN���r0)
r�smart_truncater�
_next_part�bytes_left_to_write�_writer �_write_closing_boundaryr�write_to)r!r-�part�writtenr
r
r�_load�s
zMultipartEncoder._loadcCs2yt|j�}|_Wntk
r,d}YnX|S)N)�nextrr�
StopIteration)r!r)r
r
rr2�s

zMultipartEncoder._next_partc	cs�|j}t|jd�r t|jj��}x�|D]�\}}d}d}d}t|ttf�r�t|�dkr^|\}}q�t|�dkrv|\}}}q�|\}}}}n|}tj||||d�}|j|d�|Vq&WdS)N�itemsr�)�name�data�filename�headers)�content_type)	r�hasattr�listr<�
isinstance�tupler$ZRequestFieldZmake_multipart)	r!�_fields�k�v�	file_nameZ	file_typeZfile_headersZfile_pointer�fieldr
r
r�_iter_fields�s&
zMultipartEncoder._iter_fieldscs.|j��fdd�|j�D�|_t|j�|_dS)z�This uses the fields provided by the user and creates Part objects.

        It populates the `parts` attribute and uses that to create a
        generator for iteration.
        csg|]}tj|���qSr
)�Part�
from_field)r(�f)�encr
r�
<listcomp>�sz3MultipartEncoder._prepare_parts.<locals>.<listcomp>N)rrLrrr)r!r
)rPrr�szMultipartEncoder._prepare_partscCs|jj|�S)z�Write the bytes to the end of the buffer.

        :param bytes bytes_to_write: byte-string (or bytearray) to append to
            the buffer
        :returns: int -- the number of bytes written
        )r�append)r!Zbytes_to_writer
r
rr4�szMultipartEncoder._writecCs|j|j�S)z,Write the boundary to the end of the buffer.)r4r)r!r
r
rr sz MultipartEncoder._write_boundaryc
Cs4t|j�� |jjdd�|jjd�WdQRXdS)z?Write the bytes necessary to finish a multipart/form-data body.rs--
N���)�resetr�seek�write)r!r
r
rr5sz(MultipartEncoder._write_closing_boundarycCs|jt||j��S)z/Write the current part's headers to the buffer.)r4rr)r!rAr
r
r�_write_headers
szMultipartEncoder._write_headerscCstdj|j��S)Nz!multipart/form-data; boundary={0})�strrr)r!r
r
rrBszMultipartEncoder.content_typecCs|j�S)a�Return the entirety of the data in the encoder.

        .. note::

            This simply reads all of the data it can. If you have started
            streaming or reading data from the encoder, this method will only
            return whatever data is left in the encoder.

        .. note::

            This method affects the internal state of the encoder. Calling
            this method will exhaust the encoder.

        :returns: the multipart message
        :rtype: bytes
        )�read)r!r
r
r�	to_stringszMultipartEncoder.to_stringr/cCsJ|jr|jj|�S|}|dkr4|dk	r4|jt|��}|j|�|jj|�S)z�Read data from the streaming encoder.

        :param int size: (optional), If provided, ``read`` will return exactly
            that many bytes. If it is not provided, it will return the
            remaining bytes.
        :returns: bytes
        r/Nr0)rrrYr.�intr9)r!�sizeZ
bytes_to_loadr
r
rrY+s
zMultipartEncoder.read)Nr
r0)r0)rrrr	r"�propertyr$r%r#r.r9r2rLrr4r r5rWrBrZrYr
r
r
rrs"=
+

	rcCs|S)Nr
)Zmonitorr
r
r�IDENTITY>sr^c@sFeZdZdZddd�Zeddd��Zedd	��Zd
d�Z	dd
d�Z
dS)�MultipartEncoderMonitora
    An object used to monitor the progress of a :class:`MultipartEncoder`.

    The :class:`MultipartEncoder` should only be responsible for preparing and
    streaming the data. For anyone who wishes to monitor it, they shouldn't be
    using that instance to manage that as well. Using this class, they can
    monitor an encoder and register a callback. The callback receives the
    instance of the monitor.

    To use this monitor, you construct your :class:`MultipartEncoder` as you
    normally would.

    .. code-block:: python

        from requests_toolbelt import (MultipartEncoder,
                                       MultipartEncoderMonitor)
        import requests

        def callback(monitor):
            # Do something with this information
            pass

        m = MultipartEncoder(fields={'field0': 'value0'})
        monitor = MultipartEncoderMonitor(m, callback)
        headers = {'Content-Type': monitor.content_type}
        r = requests.post('https://httpbin.org/post', data=monitor,
                          headers=headers)

    Alternatively, if your use case is very simple, you can use the following
    pattern.

    .. code-block:: python

        from requests_toolbelt import MultipartEncoderMonitor
        import requests

        def callback(monitor):
            # Do something with this information
            pass

        monitor = MultipartEncoderMonitor.from_fields(
            fields={'field0': 'value0'}, callback
            )
        headers = {'Content-Type': montior.content_type}
        r = requests.post('https://httpbin.org/post', data=monitor,
                          headers=headers)

    NcCs$||_|pt|_d|_|jj|_dS)Nr)�encoderr^�callback�
bytes_readr$)r!r`rar
r
rr"us
z MultipartEncoderMonitor.__init__�utf-8cCst|||�}|||�S)N)r)�clsrrrrar`r
r
r�from_fields�sz#MultipartEncoderMonitor.from_fieldscCs|jjS)N)r`rB)r!r
r
rrB�sz$MultipartEncoderMonitor.content_typecCs|j�S)N)rY)r!r
r
rrZ�sz!MultipartEncoderMonitor.to_stringr/cCs,|jj|�}|jt|�7_|j|�|S)N)r`rYrbr$ra)r!r\�stringr
r
rrY�s
zMultipartEncoderMonitor.read)N)NrcNr0)r0)rrrr	r"�classmethodrer]rBrZrYr
r
r
rr_Bs1
r_cCs |dkpt|t�s|j|�S|S)a6Encoding ``string`` with ``encoding`` if necessary.

    :param str string: If string is a bytes object, it will not encode it.
        Otherwise, this function will encode it with the provided encoding.
    :param str encoding: The encoding with which to encode string.
    :returns: encoded bytes object
    N)rE�bytes�encode)rfrr
r
rr�s
rcCst|d�r|St||�S)z4Coerce the data to an object with a ``read`` method.rY)rCr)r?rr
r
r�
readable_data�s
rjcCsvt|d�rt|�St|d�r"|jSt|d�r\y|j�}Wntjk
rNYnXtj|�jSt|d�rrt|j��SdS)N�__len__r$�fileno�getvalue)	rCr$rl�io�UnsupportedOperation�os�fstat�st_sizerm)�orlr
r
rr'�s



r'ccs*|j�}|jdd�dV|j|d�dS)aKeep track of the buffer's current position and write to the end.

    This is a context manager meant to be used when adding data to the buffer.
    It eliminates the need for every function to be concerned with the
    position of the cursor in the buffer.
    rrN)�tellrU)�bufferZoriginal_positionr
r
rrT�srTcCsLt|t�sHt|d�r"t|j�|�St|d�r4t|�St|d�sHt||�S|S)z5Ensure that every object's __len__ behaves uniformly.rmrlrY)rErrCrm�FileWrapper)r?rr
r
r�coerce_data�s




rwcCst|d�rt|j��St|�S)Nr<)rCrDr<)rr
r
r�to_list�s
rxc@s0eZdZdd�Zedd��Zdd�Zdd�Zd	S)
rMcCs,||_||_d|_t|j�t|j�|_dS)NT)rA�body�headers_unreadr$r')r!rAryr
r
rr"�sz
Part.__init__cCs$t|j�|�}t|j|�}|||�S)z8Create a part from a Request Field generated by urllib3.)rZrender_headersrwr?)rdrKrrAryr
r
rrN�szPart.from_fieldcCs*d}|jr|t|j�7}|t|j�dkS)z�Determine if there are bytes left to write.

        :returns: bool -- ``True`` if there are bytes left to write, otherwise
            ``False``
        r)rzr$rAr'ry)r!Zto_readr
r
rr3�szPart.bytes_left_to_writecCsrd}|jr ||j|j�7}d|_xLt|j�dkrl|dks@||krl|}|dkrT||}||j|jj|��7}q"W|S)a�Write the requested amount of bytes to the buffer provided.

        The number of bytes written may exceed size on the first read since we
        load the headers ambitiously.

        :param CustomBytesIO buffer: buffer we want to write bytes to
        :param int size: number of bytes requested to be written to the buffer
        :returns: int -- number of bytes actually written
        rFr/r0r0)rzrRrAr'ryrY)r!rur\r8Zamount_to_readr
r
rr6�s
 z
Part.write_toN)rrrr"rgrNr3r6r
r
r
rrM�srMcsBeZdZd
�fdd�	Zdd�Zedd��Zd	d
�Zdd�Z�Z	S)rN�utf-8cst||�}tt|�j|�dS)N)r�superrr")r!rur)�	__class__r
rr"s
zCustomBytesIO.__init__cCs,|j�}|jdd�|j�}|j|d�|S)Nrr)rtrU)r!Zcurrent_pos�lengthr
r
r�_get_ends
zCustomBytesIO._get_endcCs|j�}||j�S)N)rrt)r!r~r
r
rr$!szCustomBytesIO.lenc	Cs"t|��|j|�}WdQRX|S)N)rTrV)r!rhr8r
r
rrR&s
zCustomBytesIO.appendcCsRt|�}|j�|}||krN|j�}|jdd�|j�|j|�|jdd�dS)Nr)r'rrYrU�truncaterV)r!Z
to_be_readZalready_readZ	old_bytesr
r
rr1+s
zCustomBytesIO.smart_truncate)Nr{)
rrrr"rr]r$rRr1�
__classcell__r
r
)r}rrs
rc@s*eZdZdd�Zedd��Zd
dd�ZdS)rvcCs
||_dS)N)�fd)r!Zfile_objectr
r
rr"8szFileWrapper.__init__cCst|j�|jj�S)N)r'r�rt)r!r
r
rr$;szFileWrapper.lenr/cCs|jj|�S)N)r�rY)r!r~r
r
rrY?szFileWrapper.readNr0)r0)rrrr"r]r$rYr
r
r
rrv7srvc@s*eZdZdZd	dd�Zdd�Zdd�ZdS)
�FileFromURLWrappera�File from URL wrapper.

    The :class:`FileFromURLWrapper` object gives you the ability to stream file
    from provided URL in chunks by :class:`MultipartEncoder`.
    Provide a stateless solution for streaming file from one server to another.
    You can use the :class:`FileFromURLWrapper` without a session or with
    a session as demonstated by the examples below:

    .. code-block:: python
        # no session

        import requests
        from requests_toolbelt import MultipartEncoder, FileFromURLWrapper

        url = 'https://httpbin.org/image/png'
        streaming_encoder = MultipartEncoder(
            fields={
                'file': FileFromURLWrapper(url)
            }
        )
        r = requests.post(
            'https://httpbin.org/post', data=streaming_encoder,
            headers={'Content-Type': streaming_encoder.content_type}
        )

    .. code-block:: python
        # using a session

        import requests
        from requests_toolbelt import MultipartEncoder, FileFromURLWrapper

        session = requests.Session()
        url = 'https://httpbin.org/image/png'
        streaming_encoder = MultipartEncoder(
            fields={
                'file': FileFromURLWrapper(url, session=session)
            }
        )
        r = session.post(
            'https://httpbin.org/post', data=streaming_encoder,
            headers={'Content-Type': streaming_encoder.content_type}
        )

    NcCs4|p
tj�|_|j|�}t|jd�|_|j|_dS)Nzcontent-length)	�requestsZSession�session�_request_for_filer[rAr$�raw�raw_data)r!�file_urlr�Zrequested_filer
r
rr"qs
zFileFromURLWrapper.__init__cCs\|jj|dd�}|jjdd�}|dkr<dj|d�}t|��n|j�sXdj|d�}t|��|S)z&Make call for file under provided URL.T)�streamzcontent-lengthNzhData from provided URL {url} is not supported. Lack of content-length Header in requested file response.)ZurlzZData from provided URL {url} is not supported. content-length header value is not a digit.)r��getrArr�isdigit)r!r�ZresponseZcontent_lengthZ	error_msgr
r
rr�ws
z$FileFromURLWrapper._request_for_filecCs@|dkr|n|j}|jj|�p d}|j|r4t|�nd8_|S)zRead file in chunks.rr)r$r�rY)r!Z
chunk_size�chunkr
r
rrY�szFileFromURLWrapper.read)N)rrrr	r"r�rYr
r
r
rr�Cs,
r�)r	�
contextlibrnrpZuuidrr�Z_compatr�	Exceptionr�objectrr^r_rrjr'�contextmanagerrTrwrxrM�BytesIOrrvr�r
r
r
r�<module>	s,(U
2"

Filemanager

Name Type Size Permission Actions
__init__.cpython-36.opt-1.pyc File 859 B 0644
__init__.cpython-36.pyc File 859 B 0644
decoder.cpython-36.opt-1.pyc File 5.54 KB 0644
decoder.cpython-36.pyc File 5.54 KB 0644
encoder.cpython-36.opt-1.pyc File 20.38 KB 0644
encoder.cpython-36.pyc File 20.38 KB 0644