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
Provenance
- Commit
56ecdeb- Tree
5a7fc29- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent 1
14ee84b- Parent 2
704a922
5 files changed+42 added−13 removed
modified:docs/user/advanced.rst+11 added-1 removed
484 unmodified lines485 proxies = {486 "http": "http://user:pass@10.10.1.10:3128/",487 }488 489-Note that proxy URLs must include the scheme.489+To give a proxy for a specific scheme and host, use the490+`scheme://hostname` form for the key. This will match for491+any request to the given scheme and exact hostname.492 493+::494+495+ proxies = {496+ "http://10.20.1.128": "http://10.10.1.10:5323",497+ }498+499+Note that proxy URLs must include the scheme.500+ 501 .. _compliance:502 503 Compliance504 ----------373 unmodified linesmodified:requests/adapters.py+5 added-7 removed
15 unmodified lines16 from .packages.urllib3.util import Timeout as TimeoutSauce17 from .packages.urllib3.util.retry import Retry18 from .compat import urlparse, basestring19 from .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)22 from .structures import CaseInsensitiveDict23 from .packages.urllib3.exceptions import ConnectTimeoutError24 from .packages.urllib3.exceptions import HTTPError as _HTTPError25 from .packages.urllib3.exceptions import MaxRetryError212 unmodified lines238 239 :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)243 244 if proxy:245 proxy = prepend_scheme_if_needed(proxy, 'http')246 proxy_manager = self.proxy_manager_for(proxy)24 unmodified lines271 when subclassing the272 :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.273 274 :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)280-279 if proxy and scheme != 'https':280 url = urldefragauth(request.url)281 else:282 url = request.path_url150 unmodified linesmodified:requests/sessions.py+5 added-5 removed
297 unmodified lines298 #: Default Authentication tuple or object to attach to299 #: :class:`Request <Request>`.300 self.auth = None301 302- #: 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 = {}306 307 #: Event-handling hooks.308 self.hooks = default_hooks()118 unmodified lines427 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 linesmodified:requests/utils.py+12 added
535 unmodified lines536 return {}537 else:538 return getproxies()539 540+def select_proxy(url, proxies):541+ """Select a proxy for the url, if applicable.542 543+ :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 proxy552+553 def default_user_agent(name="python-requests"):554 """Return a string representing the default user agent."""555 _implementation = platform.python_implementation()556 164 unmodified linesmodified:test_requests.py+9 added
1325 unmodified lines1326 assert get_environ_proxies(1327 'http://localhost.localdomain:5000/v1.0/') == {}1328 assert get_environ_proxies('http://www.requests.com/') != {}1329 1330+ 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 None1338+1339 def test_guess_filename_when_int(self):1340 from requests.utils import guess_filename1341 assert None is guess_filename(1)1342 335 unmodified lines