Python 3.8.5 FTPS connection
Asked Answered
M

1

2

I'm trying to do a FTPS (or FTP) connection to a FTP server. This is done on Python 3.8.5 32 bit via Visual Studio Code.

Here is the code:

import ftplib
session = ftplib.FTP_TLS('server address')
#session.connect ('server address', 991)
session.login(user='username',passwd='password')
#session.prot_p()
session.set_pasv(True)
session.cwd("files")
print(session.pwd())
filename = "ftpTest.txt"
my_file = open('filepath\\ftpTest.txt', 'wb') # Open a local file to store the downloaded file
session.retrbinary('RETR ' + filename, my_file.write, 1024)

session.quit()

I am able to get the session.pwd (which display /files) but the connection timeout at line 11 (session.retrbinary) in approximately 22 sec with the following error:

Exception has occurred: TimeoutError
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I had tried setting session.set_pasv to both True and False following Python ftplib timing out. Setting it True raised the TimeoutError and setting it False raise the following error at line 11:

Exception has occurred: error_perm
500 Illegal PORT command

and also tried setting a different port (991) following Python SSL FTP connection timing out and it raised the Timeout Error at line 3.

Using FTP without TLS raised the following error at line 4 (session.login):

Exception has occurred: error_perm
530 Non-anonymous sessions must use encryption.

Turning off my McAfee LiveSafe firewall didnt help either. Btw file transfer works with Filezilla, was able to freely transfer.

Mendiola answered 6/8, 2020 at 6:18 Comment(0)
M
1

Setting up the secure data connection and changing the session af to INET6 seemed to work for me. This was suggested to me by a colleague, and as to why it works is beyond me. If anyone can provide a proper explanation, please do.

Code:

session.login(user='username',passwd='password')
session.prot_p()
session.af = socket.AF_INET6
Mendiola answered 12/8, 2020 at 2:48 Comment(1)
Possible explanation is that with IPv4, the server sends its IP address in response to the PASV command. If the server or network is not properly configured, the IP address provided may not be routable from client machine. So the file transfer fails. With with IPv6, the server does not send its IP address in the response to EPSV command, and the client uses original server IP address. See also Cannot list FTP directory using ftplib – but FTP client works.Velour

© 2022 - 2024 — McMap. All rights reserved.