This change is in response to issue #879.
# 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'):
- self.proxies = get_environ_proxies()
+ self.proxies = get_environ_proxies(self.url)
self.data = data
self.params = params
return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")
-def get_environ_proxies():
+def get_environ_proxies(url):
"""Return a dict of environment proxies."""
proxy_keys = [
'http',
'https',
'ftp',
- 'socks',
- 'no'
+ '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')
+
+ 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 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])
# Path hack.
sys.path.insert(0, os.path.abspath('..'))
+from requests.utils import get_environ_proxies
import requests.utils
from requests.compat import is_py3, bytes
byteschr = chr
-class GuessJSONUTFTests(unittest.TestCase):
+class UtilityTests(unittest.TestCase):
"""Tests for the JSON UTF encoding guessing code."""
codecs = (
continue
raise
+ def test_get_environ_proxies_respects_no_proxy(self):
+ '''This test confirms that the no_proxy environment setting is
+ respected by get_environ_proxies().'''
+
+ # Set up some example environment settings.
+ os.environ['http_proxy'] = 'http://www.example.com/'
+ os.environ['no_proxy'] = r'localhost,.0.0.1:8080'
+
+ # Set up expected proxy return values.
+ proxy_yes = {'http': 'http://www.example.com/'}
+ proxy_no = {}
+
+ # Check that we get the right things back.
+ self.assertEqual(proxy_yes,
+ get_environ_proxies('http://www.google.com/'))
+ self.assertEqual(proxy_no,
+ get_environ_proxies('http://localhost/test'))
+ self.assertEqual(proxy_no,
+ get_environ_proxies('http://127.0.0.1:8080/'))
+ self.assertEqual(proxy_yes,
+ get_environ_proxies('http://127.0.0.1:8081/'))
+
+
if __name__ == '__main__':
unittest.main()