diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 2f092d50f31782..7b78234a39911e 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -845,7 +845,10 @@ def parse229(resp, peer): if len(parts) != 5: raise error_proto(resp) host = peer[0] - port = int(parts[3]) + try: + port = int(parts[3]) + except ValueError: + raise error_proto(resp) from None return host, port diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 795527382b2abb..3a7bb5cc5d4321 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -1190,6 +1190,18 @@ def test__all__(self): 'ftpcp', 'test'} support.check__all__(self, ftplib, not_exported=not_exported) + def test_parse229(self): + peer = ('127.0.0.1', 21) + self.assertEqual( + ftplib.parse229('229 Entering Extended Passive Mode (|||6400|)', peer), + ('127.0.0.1', 6400) + ) + # Non-numeric or empty port must raise error_proto + with self.assertRaises(ftplib.error_proto): + ftplib.parse229('229 Entering Extended Passive Mode (|||invalid|)', peer) + with self.assertRaises(ftplib.error_proto): + ftplib.parse229('229 Entering Extended Passive Mode (||||)', peer) + class CommandLineTest(TestCase): """Tests for the ftplib command-line interface.""" diff --git a/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst new file mode 100644 index 00000000000000..6121032f33d9e7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst @@ -0,0 +1 @@ +``ftplib.parse229()`` now raises :exc:`ftplib.error_proto` instead of :exc:`ValueError` when encountering a malformed or non-numeric port in EPSV responses.