diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 752e2b46fdc..d15a1353d8b 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -247,12 +247,13 @@ class FTP: This does not follow the procedure from the RFC to send Telnet IP and Synch; that doesn't seem to work with the servers I've tried. Instead, just send the ABOR command as OOB data.''' - line = 'ABOR' + CRLF + line = b'ABOR' + B_CRLF if self.debugging > 1: print('*put urgent*', self.sanitize(line)) self.sock.sendall(line, MSG_OOB) resp = self.getmultiline() if resp[:3] not in {'426', '225', '226'}: raise error_proto(resp) + return resp def sendcmd(self, cmd): '''Send a command and return the response.''' @@ -816,6 +817,15 @@ else: conn.close() return self.voidresp() + def abort(self): + # overridden as we can't pass MSG_OOB flag to sendall() + line = b'ABOR' + B_CRLF + self.sock.sendall(line) + resp = self.getmultiline() + if resp[:3] not in {'426', '225', '226'}: + raise error_proto(resp) + return resp + __all__.append('FTP_TLS') all_errors = (Error, IOError, EOFError, ssl.SSLError) diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 92479d97393..78e0fb4977e 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -81,6 +81,8 @@ class DummyFTPHandler(asynchat.async_chat): def __init__(self, conn): asynchat.async_chat.__init__(self, conn) + # tells the socket to handle urgent data inline (ABOR command) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 1) self.set_terminator(b"\r\n") self.in_buffer = [] self.dtp = None @@ -202,6 +204,9 @@ class DummyFTPHandler(asynchat.async_chat): self.push('221 quit ok') self.close() + def cmd_abor(self, arg): + self.push('226 abor ok') + def cmd_stor(self, arg): self.push('125 stor ok') @@ -520,6 +525,9 @@ class TestFTPClass(TestCase): # Ensure the connection gets closed; sock attribute should be None self.assertEqual(self.client.sock, None) + def test_abort(self): + self.client.abort() + def test_retrbinary(self): def callback(data): received.append(data.decode('ascii')) diff --git a/Misc/NEWS b/Misc/NEWS index 2d56431366b..a3de216ff7d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -140,6 +140,8 @@ Core and Builtins Library ------- +- Issue #12002: ftplib's abort() method raises TypeError. + - Issue 11916: Add a number of MacOSX specific definitions to the errno module. Patch by Pierre Carrier.