Skip to content
Myr

Use urllib to retrieve environment proxies.

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

This has the added benefit of including proxies defined by the OS X System Configuration framework and in the Windows registry, rather than only checking os.environ.

authored by James Clarke · committed by James Clarke · parent 3bb13f8 · Browse files · 2 files+15 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

2 files+15 added−17 removed

  • other:+15 added−17 removed

2 files+15 added−17 removed

modified:requests/compat.py+2 added-2 removed
81 unmodified lines
82# Specifics83# ---------8485if 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 Morsel
8 unmodified lines
99100101elif 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 OrderedDict
6 unmodified lines
modified:requests/utils.py+13 added-15 removed
20 unmodified lines
21from . import __version__22from . import certs23from .compat import parse_http_list as _parse_list_header24from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse25from .compat import getproxies, proxy_bypass26from .cookies import RequestsCookieJar, cookiejar_from_dict27from .structures import CaseInsensitiveDict2829_hush_pyflakes = (RequestsCookieJar,)
356 unmodified lines
386387def get_environ_proxies(url):388 """Return a dict of environment proxies."""389389 proxy_keys = [390 'all',391 'http',392 'https',393 'ftp',394 'socks'395 ]396390 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())391392 # 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).netloc402396 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).netloc408401 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 {}412413 # 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 418420419def default_user_agent():420 """Return a string representing the default user agent."""421 _implementation = platform.python_implementation()422
126 unmodified lines