Merge pull request #945 from Lukasa/develop

Respect the no_proxy environment variable.
This commit is contained in:
Kenneth Reitz
2012-11-23 02:03:28 -08:00
3 changed files with 68 additions and 5 deletions
+1 -1
View File
@@ -119,7 +119,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'):
self.proxies = get_environ_proxies()
self.proxies = get_environ_proxies(self.url)
self.data = data
self.params = params
+21 -3
View File
@@ -501,7 +501,7 @@ def requote_uri(uri):
return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")
def get_environ_proxies():
def get_environ_proxies(url):
"""Return a dict of environment proxies."""
proxy_keys = [
@@ -509,11 +509,29 @@ def get_environ_proxies():
'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])
+46 -1
View File
@@ -9,6 +9,7 @@ import random
# 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
@@ -20,7 +21,7 @@ else:
byteschr = chr
class GuessJSONUTFTests(unittest.TestCase):
class UtilityTests(unittest.TestCase):
"""Tests for the JSON UTF encoding guessing code."""
codecs = (
@@ -73,5 +74,49 @@ class GuessJSONUTFTests(unittest.TestCase):
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().'''
# Store the current environment settings.
try:
old_http_proxy = os.environ['http_proxy']
except KeyError:
old_http_proxy = None
try:
old_no_proxy = os.environ['no_proxy']
except KeyError:
old_no_proxy = None
# 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/'))
# Return the settings to what they were.
if old_http_proxy:
os.environ['http_proxy'] = old_http_proxy
else:
del os.environ['http_proxy']
if old_no_proxy:
os.environ['no_proxy'] = old_no_proxy
else:
del os.environ['no_proxy']
if __name__ == '__main__':
unittest.main()