diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 8093f0e2f22..f1c478c2deb 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -947,6 +947,22 @@ class HandlerTests(unittest.TestCase): self.assertEqual([(handlers[0], "http_open")], [tup[0:2] for tup in o.calls]) + def test_proxy_no_proxy(self): + os.environ['no_proxy'] = 'python.org' + o = OpenerDirector() + ph = urllib.request.ProxyHandler(dict(http="proxy.example.com")) + o.add_handler(ph) + req = Request("http://www.perl.org/") + self.assertEqual(req.get_host(), "www.perl.org") + r = o.open(req) + self.assertEqual(req.get_host(), "proxy.example.com") + req = Request("http://www.python.org") + self.assertEqual(req.get_host(), "www.python.org") + r = o.open(req) + self.assertEqual(req.get_host(), "www.python.org") + del os.environ['no_proxy'] + + def test_proxy_https(self): o = OpenerDirector() ph = urllib.request.ProxyHandler(dict(https="proxy.example.com:3128")) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index f787407bfbc..9f7ebd7afce 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -657,6 +657,10 @@ class ProxyHandler(BaseHandler): proxy_type, user, password, hostport = _parse_proxy(proxy) if proxy_type is None: proxy_type = orig_type + + if req.host and proxy_bypass(req.host): + return None + if user and password: user_pass = '%s:%s' % (unquote(user), unquote(password)) diff --git a/Misc/NEWS b/Misc/NEWS index 49e7d1a3708..f6660a1187a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,8 @@ Core and Builtins Library ------- +- Issue #6894: Fixed the issue urllib2 doesn't respect "no_proxy" environment + - Issue #7082: When falling back to the MIME 'name' parameter, the correct place to look for it is the Content-Type header.