From bc07ac51801ae261ce09f057cbb4ecf9b7cdc07c Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Tue, 22 Jul 2014 00:15:20 -0700 Subject: [PATCH] Fix localhost checking in FileHandler. Raised in #21970. --- Lib/test/test_urllib2.py | 3 ++- Lib/urllib/request.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 6b73723a6e3..76208768a5b 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -678,7 +678,7 @@ class HandlerTests(unittest.TestCase): self.assertEqual(int(headers["Content-length"]), len(data)) def test_file(self): - import email.utils, socket + import email.utils h = urllib.request.FileHandler() o = h.parent = MockOpener() @@ -725,6 +725,7 @@ class HandlerTests(unittest.TestCase): for url in [ "file://localhost:80%s" % urlpath, "file:///file_does_not_exist.txt", + "file://not-a-local-host.com//dir/file.txt", "file://%s:80%s/%s" % (socket.gethostbyname('localhost'), os.getcwd(), TESTFN), "file://somerandomhost.ontheinternet.com%s/%s" % diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 72e91dd5f4c..0389f5e853c 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1315,7 +1315,7 @@ class FileHandler(BaseHandler): url = req.selector if url[:2] == '//' and url[2:3] != '/' and (req.host and req.host != 'localhost'): - if not req.host is self.get_names(): + if not req.host in self.get_names(): raise URLError("file:// scheme is supported only on localhost") else: return self.open_local_file(req)