Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbc4782

Browse files
committed
fix issue 9601: ftplib now provides a workaround for invalid response code returned on MKD and PWD by non-compliant FTPserver implementations such as ISS shipped with Windows server 2003
1 parent 076e031 commit bbc4782

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

Lib/ftplib.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,11 @@ def size(self, filename):
565565

566566
def mkd(self, dirname):
567567
'''Make a directory, return its full pathname.'''
568-
resp = self.sendcmd('MKD ' + dirname)
568+
resp = self.voidcmd('MKD ' + dirname)
569+
# fix around non-compliant implementations such as IIS shipped
570+
# with Windows server 2003
571+
if not resp.startswith('257'):
572+
return ''
569573
return parse257(resp)
570574

571575
def rmd(self, dirname):
@@ -574,7 +578,11 @@ def rmd(self, dirname):
574578

575579
def pwd(self):
576580
'''Return current working directory.'''
577-
resp = self.sendcmd('PWD')
581+
resp = self.voidcmd('PWD')
582+
# fix around non-compliant implementations such as IIS shipped
583+
# with Windows server 2003
584+
if not resp.startswith('257'):
585+
return ''
578586
return parse257(resp)
579587

580588
def quit(self):

Lib/test/test_ftplib.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,18 @@ def is_client_connected():
606606
self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
607607
self.assertFalse(is_client_connected())
608608

609+
def test_parse257(self):
610+
self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar')
611+
self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar')
612+
self.assertEqual(ftplib.parse257('257 ""'), '')
613+
self.assertEqual(ftplib.parse257('257 "" created'), '')
614+
self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"')
615+
# The 257 response is supposed to include the directory
616+
# name and in case it contains embedded double-quotes
617+
# they must be doubled (see RFC-959, chapter 7, appendix 2).
618+
self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar')
619+
self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar')
620+
609621

610622
class TestIPv6Environment(TestCase):
611623

Misc/NEWS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ Extensions
123123
Library
124124
-------
125125

126+
- Issue #9601: ftplib now provides a workaround for non-compliant
127+
implementations such as IIS shipped with Windows server 2003 returning invalid
128+
response codes for MKD and PWD commands.
129+
126130
- Issue #658749: asyncore's connect() method now correctly interprets winsock
127-
errors;
131+
errors.
128132

129133
- Issue #9501: Fixed logging regressions in cleanup code.
130134

@@ -291,6 +295,8 @@ Tools/Demos
291295
Tests
292296
-----
293297

298+
- Issue #9601: Provide a test case for ftplib.parse257.
299+
294300
- Issue #8857: Provide a test case for socket.getaddrinfo.
295301

296302
- Issue #7564: Skip test_ioctl if another process is attached to /dev/tty.

0 commit comments

Comments
 (0)