diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 50771e8c17c250..2f2b0078a5577f 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -324,7 +324,11 @@ def makeport(self): def makepasv(self): """Internal: Does the PASV or EPSV handshake -> (address, port)""" if self.af == socket.AF_INET: - untrusted_host, port = parse227(self.sendcmd('PASV')) + pasv_response = self.sendcmd('PASV') + try: + untrusted_host, port = parse227(pasv_response) + except error_reply: + untrusted_host, port = parse229(pasv_response, self.sock.getpeername()) if self.trust_server_pasv_ipv4_address: host = untrusted_host else: diff --git a/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst new file mode 100644 index 00000000000000..0cc4fc1a92d6d2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-01-16-43-38.gh-issue-127478.fHI_y9.rst @@ -0,0 +1 @@ +Enhancement to support FTP connection with NAT scenario.