diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 9071f4989e0f..9d1ed9b616ab 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -736,6 +736,8 @@ def test_file(self): ("file://ftp.example.com///foo.txt", False), # XXXX bug: fails with OSError, should be URLError ("file://ftp.example.com/foo.txt", False), + ("file://somehost//foo/something.txt", True), + ("file://localhost//foo/something.txt", False), ]: req = Request(url) try: @@ -746,6 +748,7 @@ def test_file(self): else: self.assertTrue(o.req is req) self.assertEqual(req.type, "ftp") + self.assertEqual(req.type is "ftp", ftp) def test_http(self): diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index e59208ff85fe..cb80a36f8ba0 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1188,7 +1188,8 @@ class FileHandler(BaseHandler): # Use local file or FTP depending on form of URL def file_open(self, req): url = req.selector - if url[:2] == '//' and url[2:3] != '/': + if url[:2] == '//' and url[2:3] != '/' and (req.host and + req.host != 'localhost'): req.type = 'ftp' return self.parent.open(req) else: