Merge pull request #1400 from jam/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Retrieve environment proxies using standard library functions
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 8430e6c, f705c41 · Browse files · 3 files·+16 added−17 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+16 added−17 removed
- other:+16 added−17 removed
Provenance
- Commit
8028103- Tree
91dd368- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
8430e6c- Parent 2
f705c41
3 files changed+16 added−17 removed
modified:requests/compat.py+2 added-2 removed
81 unmodified lines82 # Specifics83 # ---------84 85 if is_py2:86- from urllib import quote, unquote, quote_plus, unquote_plus, urlencode86+ from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass87 from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag88 from urllib2 import parse_http_list89 import cookielib90 from Cookie import Morsel8 unmodified lines99 100 101 elif is_py3:102 from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag103- from urllib.request import parse_http_list103+ from urllib.request import parse_http_list, getproxies, proxy_bypass104 from http import cookiejar as cookielib105 from http.cookies import Morsel106 from io import StringIO107 from collections import OrderedDict6 unmodified linesmodified:requests/utils.py+13 added-15 removed
20 unmodified lines21 from . import __version__22 from . import certs23 from .compat import parse_http_list as _parse_list_header24 from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse25+from .compat import getproxies, proxy_bypass26 from .cookies import RequestsCookieJar, cookiejar_from_dict27 from .structures import CaseInsensitiveDict28 29 _hush_pyflakes = (RequestsCookieJar,)356 unmodified lines386 387 def get_environ_proxies(url):388 """Return a dict of environment proxies."""389 389- proxy_keys = [390- 'all',391- 'http',392- 'https',393- 'ftp',394- 'socks'395- ]396-390 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())391 392 # First check whether no_proxy is defined. If it is, check that the URL393 # we're getting isn't in the no_proxy list.394 no_proxy = get_proxy('no_proxy')395+ netloc = urlparse(url).netloc402-396+ 397 if no_proxy:398 # We need to check whether we match here. We need to see if we match399 # the end of the netloc, both with and without the port.400 no_proxy = no_proxy.split(',')407- netloc = urlparse(url).netloc408-401+ 402 for host in no_proxy:403 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):404 # The URL does match something in no_proxy, so we don't want405 # to apply the proxies on this URL.406 return {}407+ 408+ # If the system proxy settings indicate that this URL should be bypassed,409+ # don't proxy.410+ if proxy_bypass(netloc):411+ return {}412 413 # If we get here, we either didn't have no_proxy set or we're not going416- # anywhere that no_proxy applies to.417- proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys]418- return dict([(key, val) for (key, val) in proxies if val])414+ # anywhere that no_proxy applies to, and the system settings don't require415+ # bypassing the proxy for the current URL.416+ return getproxies()417+ 418 420-419 def default_user_agent():420 """Return a string representing the default user agent."""421 _implementation = platform.python_implementation()422 126 unmodified linesmodified:AUTHORS.rst+1 added
127 unmodified lines128 - Bob Carroll <bob.carroll@alum.rit.edu> @rcarz129 - Hugo Osvaldo Barrera <hugo@osvaldobarrera.com.ar> @hobarrera130 - Łukasz Langa <lukasz@langa.pl> @llanga131 - Dave Shawley <daveshawley@gmail.com>132+- James Clarke (jam)