Skip to content
Myr

Merge pull request #3865 from JohnVillalovos/master

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

Allow use of 'no_proxy' in the proxies argument

authored by Kenneth Reitz · committed by GitHub · parents 4533979, 85400d8 · Browse files · 3 files+91 added−15 removed
This commit

State

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

Diff stats

3 files+91 added−15 removed

  • tests:+49 added−3 removed
  • other:+42 added−12 removed

3 files+91 added−15 removed

modified:requests/sessions.py+8 added-4 removed
234 unmodified lines
235 necessary.236237 :rtype: dict238 """239 proxies = proxies if proxies is not None else {}240 headers = prepared_request.headers241 url = prepared_request.url242 scheme = urlparse(url).scheme242 new_proxies = proxies.copy() if proxies is not None else {}243 new_proxies = proxies.copy()244 no_proxy = proxies.get('no_proxy')245246 bypass_proxy = should_bypass_proxies(url, no_proxy=no_proxy)244 if self.trust_env and not should_bypass_proxies(url):245 environ_proxies = get_environ_proxies(url)247 if self.trust_env and not bypass_proxy:248 environ_proxies = get_environ_proxies(url, no_proxy=no_proxy)249250 proxy = environ_proxies.get(scheme, environ_proxies.get('all'))251252 if proxy:
405 unmodified lines
658 """659 # Gather clues from the surrounding environment.660 if self.trust_env:661 # Set environment's proxies.662 no_proxy = proxies.get('no_proxy') if proxies is not None else None659 env_proxies = get_environ_proxies(url) or {}663 env_proxies = get_environ_proxies(url, no_proxy=no_proxy)664 for (k, v) in env_proxies.items():665 proxies.setdefault(k, v)666667 # Look for requests environment configuration and be compatible
67 unmodified lines
modified:requests/utils.py+34 added-8 removed
9 unmodified lines
1011import cgi12import codecs13import collections14import contextlib15import io16import os17import re18import socket
535 unmodified lines
554 return False555 return True556557558@contextlib.contextmanager559def set_environ(env_name, value):560 """Set the environment variable 'env_name' to 'value'561562 Save previous value, yield, and then restore the previous value stored in563 the environment variable 'env_name'.564565 If 'value' is None, do nothing"""566 if value is not None:567 old_value = os.environ.get(env_name)568 os.environ[env_name] = value569 try:570 yield571 finally:572 if value is None:573 return574 if old_value is None:575 del os.environ[env_name]576 else:577 os.environ[env_name] = old_value578579557def should_bypass_proxies(url):580def should_bypass_proxies(url, no_proxy):581 """582 Returns whether we should bypass proxies or not.583584 :rtype: bool
1 unmodified line
586 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())587588 # First check whether no_proxy is defined. If it is, check that the URL589 # we're getting isn't in the no_proxy list.590 no_proxy_arg = no_proxy591 if no_proxy is None:567 no_proxy = get_proxy('no_proxy')592 no_proxy = get_proxy('no_proxy')593 netloc = urlparse(url).netloc594595 if no_proxy:596 # We need to check whether we match here. We need to see if we match
24 unmodified lines
621 # The proxy_bypass function is incredibly buggy on OS X in early versions622 # of Python 2.6, so allow this call to fail. Only catch the specific623 # exceptions we've seen, though: this call failing in other ways can reveal624 # legitimate problems.625 with set_environ('no_proxy', no_proxy_arg):600 try:601 bypass = proxy_bypass(netloc)602 except (TypeError, socket.gaierror):603 bypass = False626 try:627 bypass = proxy_bypass(netloc)628 except (TypeError, socket.gaierror):629 bypass = False630631 if bypass:632 return True633634 return False635636611def get_environ_proxies(url):637def get_environ_proxies(url, no_proxy):638 """639 Return a dict of environment proxies.640641 :rtype: dict642 """617 if should_bypass_proxies(url):643 if should_bypass_proxies(url, no_proxy=no_proxy):644 return {}645 else:646 return getproxies()647
206 unmodified lines
modified:tests/test_utils.py+49 added-3 removed
159 unmodified lines
160 'http://172.16.1.1:5000/',161 'http://localhost.localdomain:5000/v1.0/',162 ))163 def test_bypass(self, url):164 assert get_environ_proxies(url) == {}164 assert get_environ_proxies(url, no_proxy=None) == {}165166 @pytest.mark.parametrize(167 'url', (168 'http://192.168.1.1:5000/',169 'http://192.168.1.1/',170 'http://www.requests.com/',171 ))172 def test_not_bypass(self, url):173 assert get_environ_proxies(url) != {}173 assert get_environ_proxies(url, no_proxy=None) != {}174175 @pytest.mark.parametrize(176 'url', (177 'http://192.168.1.1:5000/',178 'http://192.168.1.1/',179 'http://www.requests.com/',180 ))181 def test_bypass_no_proxy_keyword(self, url):182 no_proxy = '192.168.1.1,requests.com'183 assert get_environ_proxies(url, no_proxy=no_proxy) == {}184185 @pytest.mark.parametrize(186 'url', (187 'http://192.168.0.1:5000/',188 'http://192.168.0.1/',189 'http://172.16.1.1/',190 'http://172.16.1.1:5000/',191 'http://localhost.localdomain:5000/v1.0/',192 ))193 def test_not_bypass_no_proxy_keyword(self, url, monkeypatch):194 # This is testing that the 'no_proxy' argument overrides the195 # environment variable 'no_proxy'196 monkeypatch.setenv('http_proxy', 'http://proxy.example.com:3128/')197 no_proxy = '192.168.1.1,requests.com'198 assert get_environ_proxies(url, no_proxy=no_proxy) != {}199200201class TestIsIPv4Address:202203 def test_valid(self):204 assert is_ipv4_address('8.8.8.8')
344 unmodified lines
549 can be bypassed or not550 """551 monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1')552 monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1')528 assert should_bypass_proxies(url) == expected553 assert should_bypass_proxies(url, no_proxy=None) == expected554555556@pytest.mark.parametrize(557 'cookiejar', (
19 unmodified lines
577 )578)579def test_unicode_is_ascii(value, expected):580 assert unicode_is_ascii(value) is expected581582583@pytest.mark.parametrize(584 'url, expected', (585 ('http://192.168.0.1:5000/', True),586 ('http://192.168.0.1/', True),587 ('http://172.16.1.1/', True),588 ('http://172.16.1.1:5000/', True),589 ('http://localhost.localdomain:5000/v1.0/', True),590 ('http://172.16.1.12/', False),591 ('http://172.16.1.12:5000/', False),592 ('http://google.com:5000/v1.0/', False),593 ))594def test_should_bypass_proxies_no_proxy(595 url, expected, monkeypatch):596 """Tests for function should_bypass_proxies to check if proxy597 can be bypassed or not using the 'no_proxy' argument598 """599 no_proxy = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'600 # Test 'no_proxy' argument601 assert should_bypass_proxies(url, no_proxy=no_proxy) == expected