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 <crodjer@gmail.com>
This commit is contained in:
Rohan Jain
2012-05-04 10:41:57 +05:30
parent 317f64a11f
commit 0ba8c44260
+16
View File
@@ -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])