diff -r f4c6dab59cd8 Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Tue Apr 26 17:04:18 2016 +0200 +++ b/Lib/test/test_urllib.py Wed Apr 27 17:07:14 2016 +0800 @@ -231,6 +231,18 @@ self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com:8888')) self.assertTrue(urllib.request.proxy_bypass_environment('newdomain.com:1234')) + def test_proxy_bypass_environment_host_match(self): + bypass = urllib.request.proxy_bypass_environment + self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com:1234') + self.assertTrue(bypass('localhost')) + self.assertTrue(bypass('LocalHost')) # MixedCase + self.assertTrue(bypass('LOCALHOST')) # UPPERCASE + self.assertTrue(bypass('newdomain.com:1234')) + self.assertTrue(bypass('anotherdomain.com:8888')) + self.assertTrue(bypass('www.newdomain.com:1234')) + self.assertFalse(bypass('prelocalhost')) + self.assertFalse(bypass('newdomain.com')) # no port + self.assertFalse(bypass('newdomain.com:1235')) # wrong port class ProxyTests_withOrderedEnv(unittest.TestCase): diff -r f4c6dab59cd8 Lib/urllib/request.py --- a/Lib/urllib/request.py Tue Apr 26 17:04:18 2016 +0200 +++ b/Lib/urllib/request.py Wed Apr 27 17:07:14 2016 +0800 @@ -2492,8 +2492,11 @@ # check if the host ends with any of the DNS suffixes no_proxy_list = [proxy.strip() for proxy in no_proxy.split(',')] for name in no_proxy_list: - if name and (hostonly.endswith(name) or host.endswith(name)): - return 1 + if name: + name = re.escape(name) + pattern = '(.+\\.)?%s$' % name + if re.match(pattern, hostonly, re.I) or re.match(pattern, host, re.I): + return 1 # otherwise, don't bypass return 0