Skip to content
Myr

Merge pull request #2741 from jasongrout/per-host-proxy

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Implement per-host proxies

authored by Cory Benfield · committed by Cory Benfield · parents 14ee84b, 704a922 · Browse files · 5 files+42 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+42 added−13 removed

  • other:+42 added−13 removed

5 files+42 added−13 removed

modified:docs/user/advanced.rst+11 added-1 removed
484 unmodified lines
485 proxies = {486 "http": "http://user:pass@10.10.1.10:3128/",487 }488489Note that proxy URLs must include the scheme.489To give a proxy for a specific scheme and host, use the490`scheme://hostname` form for the key. This will match for491any request to the given scheme and exact hostname.492493::494495 proxies = {496 "http://10.20.1.128": "http://10.10.1.10:5323",497 }498499Note that proxy URLs must include the scheme.500 501.. _compliance:502503Compliance504----------
373 unmodified lines
modified:requests/adapters.py+5 added-7 removed
15 unmodified lines
16from .packages.urllib3.util import Timeout as TimeoutSauce17from .packages.urllib3.util.retry import Retry18from .compat import urlparse, basestring19from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,20 prepend_scheme_if_needed, get_auth_from_url, urldefragauth)20 prepend_scheme_if_needed, get_auth_from_url, urldefragauth,21 select_proxy)22from .structures import CaseInsensitiveDict23from .packages.urllib3.exceptions import ConnectTimeoutError24from .packages.urllib3.exceptions import HTTPError as _HTTPError25from .packages.urllib3.exceptions import MaxRetryError
212 unmodified lines
238239 :param url: The URL to connect to.240 :param proxies: (optional) A Requests-style dictionary of proxies used on this request.241 """241 proxies = proxies or {}242 proxy = proxies.get(urlparse(url.lower()).scheme)242 proxy = select_proxy(url, proxies)243244 if proxy:245 proxy = prepend_scheme_if_needed(proxy, 'http')246 proxy_manager = self.proxy_manager_for(proxy)
24 unmodified lines
271 when subclassing the272 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.273274 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.275 :param proxies: A dictionary of schemes to proxy URLs.275 :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs.276 """277 proxies = proxies or {}277 proxy = select_proxy(request.url, proxies)278 scheme = urlparse(request.url).scheme279 proxy = proxies.get(scheme)280279 if proxy and scheme != 'https':280 url = urldefragauth(request.url)281 else:282 url = request.path_url
150 unmodified lines
modified:requests/sessions.py+5 added-5 removed
297 unmodified lines
298 #: Default Authentication tuple or object to attach to299 #: :class:`Request <Request>`.300 self.auth = None301302 #: Dictionary mapping protocol to the URL of the proxy (e.g.303 #: {'http': 'foo.bar:3128'}) to be used on each304 #: :class:`Request <Request>`.302 #: Dictionary mapping protocol or protocol and host to the URL of the proxy303 #: (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) to304 #: be used on each :class:`Request <Request>`.305 self.proxies = {}306307 #: Event-handling hooks.308 self.hooks = default_hooks()
118 unmodified lines
427 read timeout) <timeouts>` tuple.428 :type timeout: float or tuple429 :param allow_redirects: (optional) Set to True by default.430 :type allow_redirects: bool431 :param proxies: (optional) Dictionary mapping protocol to the URL of432 the proxy.431 :param proxies: (optional) Dictionary mapping protocol or protocol and432 hostname to the URL of the proxy.433 :param stream: (optional) whether to immediately download the response434 content. Defaults to ``False``.435 :param verify: (optional) if ``True``, the SSL cert will be verified.436 A CA_BUNDLE path can also be provided.
246 unmodified lines
modified:requests/utils.py+12 added
535 unmodified lines
536 return {}537 else:538 return getproxies()539540def select_proxy(url, proxies):541 """Select a proxy for the url, if applicable.542543 :param url: The url being for the request544 :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs545 """546 proxies = proxies or {}547 urlparts = urlparse(url)548 proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)549 if proxy is None:550 proxy = proxies.get(urlparts.scheme)551 return proxy552553def default_user_agent(name="python-requests"):554 """Return a string representing the default user agent."""555 _implementation = platform.python_implementation()556
164 unmodified lines
modified:test_requests.py+9 added
1325 unmodified lines
1326 assert get_environ_proxies(1327 'http://localhost.localdomain:5000/v1.0/') == {}1328 assert get_environ_proxies('http://www.requests.com/') != {}13291330 def test_select_proxies(self):1331 """Make sure we can select per-host proxies correctly."""1332 from requests.utils import select_proxy1333 proxies = {'http': 'http://http.proxy',1334 'http://some.host': 'http://some.host.proxy'}1335 assert select_proxy('hTTp://u:p@Some.Host/path', proxies) == 'http://some.host.proxy'1336 assert select_proxy('hTTp://u:p@Other.Host/path', proxies) == 'http://http.proxy'1337 assert select_proxy('hTTps://Other.Host', proxies) is None13381339 def test_guess_filename_when_int(self):1340 from requests.utils import guess_filename1341 assert None is guess_filename(1)1342
335 unmodified lines