From 0ba8c44260209ff518809a6b5b307d443e8dfe67 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Fri, 4 May 2012 10:41:57 +0530 Subject: [PATCH 1/3] A utility function to fetch environment proxies This adds support for lowercase environment proxy variables (which are quite popular too). It returns proxies in a format compatible with request's proxy parameter. Moreover, it can be used in the request models for proxy defaults. Signed-off-by: Rohan Jain --- requests/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/requests/utils.py b/requests/utils.py index ecfee47b..8365cc36 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -446,3 +446,19 @@ def requote_uri(uri): # Then quote only illegal characters (do not quote reserved, unreserved, # or '%') return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~") + +def get_environ_proxies(): + """Return a dict of environment proxies.""" + + proxy_keys = [ + 'all', + 'http', + 'https', + 'ftp', + 'socks', + 'no' + ] + + get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper()) + proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys] + return dict([(key, val) for (key, val) in proxies if val]) From abc38cf17f4e7149cff2da96fa364e4a5d39a632 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Fri, 4 May 2012 10:44:07 +0530 Subject: [PATCH 2/3] Use utility for environment proxy as fallback Signed-off-by: Rohan Jain --- requests/models.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/requests/models.py b/requests/models.py index 8319525e..b2d0ed5f 100644 --- a/requests/models.py +++ b/requests/models.py @@ -28,7 +28,7 @@ from .exceptions import ( URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL) from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, - stream_decode_response_unicode, get_netrc_auth, + stream_decode_response_unicode, get_netrc_auth, get_environ_proxies, DEFAULT_CA_BUNDLE_PATH) from .compat import ( cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes, @@ -112,10 +112,7 @@ class Request(object): # If no proxies are given, allow configuration by environment variables # HTTP_PROXY and HTTPS_PROXY. if not self.proxies and self.config.get('trust_env'): - if 'HTTP_PROXY' in os.environ: - self.proxies['http'] = os.environ['HTTP_PROXY'] - if 'HTTPS_PROXY' in os.environ: - self.proxies['https'] = os.environ['HTTPS_PROXY'] + self.proxies = get_environ_proxies() self.data = data self.params = params From 7cf6ca80a65d56d551101e74bcdc3d03e2df7736 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Fri, 4 May 2012 10:59:57 +0530 Subject: [PATCH 3/3] Add myself to authors Signed-off-by: Rohan Jain --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 2f7de0e1..a4b2411b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -97,3 +97,4 @@ Patches and Suggestions - Jonty Wareing - Shivaram Lingamneni - Miguel Turner +- Rohan Jain (crodjer)