
ˀX/
                 @   sF   d  Z  d d l m Z d d l m Z m Z Gd d   d e  Z d S)z
requests_toolbelt.source_adapter
================================

This file contains an implementation of the SourceAddressAdapter originally
demonstrated on the Requests GitHub page.
    )HTTPAdapter   )poolmanager
basestringc                   sI   e  Z d  Z d Z   f d d   Z d d d  Z   f d d   Z   S)	SourceAddressAdaptera9  
    A Source Address Adapter for Python Requests that enables you to choose the
    local address to bind to. This allows you to send your HTTP requests from a
    specific interface and IP address.

    Two address formats are accepted. The first is a string: this will set the
    local IP address to the address given in the string, and will also choose a
    semi-random high port for the local port number.

    The second is a two-tuple of the form (ip address, port): for example,
    ``('10.10.10.10', 8999)``. This will set the local IP address to the first
    element, and the local port to the second element. If ``0`` is used as the
    port number, a semi-random high port will be selected.

    .. warning:: Setting an explicit local port can have negative interactions
                 with connection-pooling in Requests: in particular, it risks
                 the possibility of getting "Address in use" errors. The
                 string-only argument is generally preferred to the tuple-form.

    Example usage:

    .. code-block:: python

        import requests
        from requests_toolbelt.adapters.source import SourceAddressAdapter

        s = requests.Session()
        s.mount('http://', SourceAddressAdapter('10.10.10.10'))
        s.mount('https://', SourceAddressAdapter(('10.10.10.10', 8999))
    c                sb   t  | t  r! | d f |  _ n' t  | t  r< | |  _ n t d   t t |   j |   d  S)Nr   z<source_address must be IP address string or (ip, port) tuple)
isinstancer   source_addresstuple	TypeErrorsuperr   __init__)selfr   kwargs)	__class__ C/usr/lib/python3/dist-packages/requests_toolbelt/adapters/source.pyr   -   s    	zSourceAddressAdapter.__init__Fc          	   C   s.   t  j d | d | d | d |  j  |  _  d  S)NZ	num_poolsmaxsizeblockr   )r   ZPoolManagerr   )r   Zconnectionsr   r   r   r   r   init_poolmanager9   s
    	z%SourceAddressAdapter.init_poolmanagerc                s&   |  j  | d <t t |   j | |   S)Nr   )r   r   r   proxy_manager_for)r   argsr   )r   r   r   r   @   s    z&SourceAddressAdapter.proxy_manager_for)__name__
__module____qualname____doc__r   r   r   r   r   )r   r   r      s   r   N)r   Zrequests.adaptersr   Z_compatr   r   r   r   r   r   r   <module>   s   