diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py index a4b4d92503..ddb79d9f53 100644 --- a/Lib/test/test_urllibnet.py +++ b/Lib/test/test_urllibnet.py @@ -1,3 +1,4 @@ +import tempfile import unittest from test import test_support @@ -213,6 +214,32 @@ class urlopen_HttpsTests(unittest.TestCase): self.assertIn("Python", response.read()) +class urlopen_FTPTest(unittest.TestCase): + FTP_TEST_FILE = 'ftp://ftp.debian.org/debian/README' + + def test_multiple_ftp_retrieves(self): + + with test_support.transient_internet(self.FTP_TEST_FILE): + try: + for _ in range(3): + urllib.FancyURLopener().retrieve(self.FTP_TEST_FILE, + tempfile.NamedTemporaryFile().name) + + except IOError as e: + self.fail("Failed FTP retrieve while accessing ftp url " + "multiple times.\n Error message was : %s" % e) + + def test_multiple_ftp_urlopen_same_host(self): + with test_support.transient_internet(self.FTP_TEST_FILE): + try: + for _ in range(3): + fp = urllib.urlopen(self.FTP_TEST_FILE) + # closing fp is not required. + except IOError as e: + self.fail("Failed FTP binary file open." + "Error message was: %s" % e) + + def test_main(): test_support.requires('network') with test_support.check_py3k_warnings( @@ -220,7 +247,8 @@ def test_main(): test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests, urlretrieveNetworkTests, - urlopen_HttpsTests) + urlopen_HttpsTests, + urlopen_FTPTest) if __name__ == "__main__": test_main() diff --git a/Lib/urllib.py b/Lib/urllib.py index c3c8ef4b60..2038c43528 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -932,7 +932,13 @@ class ftpwrapper: return (ftpobj, retrlen) def endtransfer(self): + if not self.busy: + return self.busy = 0 + try: + self.ftp.voidresp() + except ftperrors(): + pass def close(self): self.keepalive = False