From: James Clarke Date: Sat, 1 Jun 2013 01:19:34 +0000 (-0700) Subject: Use urllib to retrieve environment proxies. X-Git-Tag: 2.0~19^2~10^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=93be6916f98f191eee9ac053994f61e99869735b;p=services%2Fpython-requests.git Use urllib to retrieve environment proxies. 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. --- diff --git a/requests/compat.py b/requests/compat.py index bcf94b0..e5f6e1d 100644 --- a/requests/compat.py +++ b/requests/compat.py @@ -83,7 +83,7 @@ except ImportError: # --------- if is_py2: - from urllib import quote, unquote, quote_plus, unquote_plus, urlencode + from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag from urllib2 import parse_http_list import cookielib @@ -100,7 +100,7 @@ if is_py2: elif is_py3: from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag - from urllib.request import parse_http_list + from urllib.request import parse_http_list, getproxies, proxy_bypass from http import cookiejar as cookielib from http.cookies import Morsel from io import StringIO diff --git a/requests/utils.py b/requests/utils.py index b21bf8f..ea87c61 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -22,6 +22,7 @@ from . import __version__ from . import certs from .compat import parse_http_list as _parse_list_header from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse +from .compat import getproxies, proxy_bypass from .cookies import RequestsCookieJar, cookiejar_from_dict from .structures import CaseInsensitiveDict @@ -386,37 +387,34 @@ def requote_uri(uri): def get_environ_proxies(url): """Return a dict of environment proxies.""" - proxy_keys = [ - 'all', - 'http', - 'https', - 'ftp', - 'socks' - ] - get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper()) # First check whether no_proxy is defined. If it is, check that the URL # we're getting isn't in the no_proxy list. no_proxy = get_proxy('no_proxy') - + netloc = urlparse(url).netloc + if no_proxy: # We need to check whether we match here. We need to see if we match # the end of the netloc, both with and without the port. no_proxy = no_proxy.split(',') - netloc = urlparse(url).netloc - + for host in no_proxy: if netloc.endswith(host) or netloc.split(':')[0].endswith(host): # The URL does match something in no_proxy, so we don't want # to apply the proxies on this URL. return {} + + # If the system proxy settings indicate that this URL should be bypassed, + # don't proxy. + if proxy_bypass(netloc): + return {} # If we get here, we either didn't have no_proxy set or we're not going - # anywhere that no_proxy applies to. - proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys] - return dict([(key, val) for (key, val) in proxies if val]) - + # anywhere that no_proxy applies to, and the system settings don't require + # bypassing the proxy for the current URL. + return getproxies() + def default_user_agent(): """Return a string representing the default user agent."""