I am trying to transfer files to a FTP implicit TLS server using python. I can connect to the server, I can upload the file, but at the end, I am having an exception.
Here is my code. First part is the class.
import ftplib
import ssl
class ImplicitFTP_TLS(ftplib.FTP_TLS):
"""FTP_TLS subclass that automatically wraps sockets in SSL to support implicit FTPS."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sock = None
#self.verify_mode(False)
@property
def sock(self):
"""Return the socket."""
return self._sock
@sock.setter
def sock(self, value):
"""When modifying the socket, ensure that it is ssl wrapped."""
if value is not None and not isinstance(value, ssl.SSLSocket):
value = self.context.wrap_socket(value)
self._sock = value
The second part is to connect and send the file.
ftp_client = ImplicitFTP_TLS()
ftp_client.connect(host='ip_address', port=990)
ftp_client.login(user='username', passwd='my_password')
ftp_client.prot_p()
ftp_client.dir()
tmp_file_name = "my_file.csv"
tmp_file = open(tmp_file_name,'rb')
destination_path = '/my_destination_file.csv'
remote_path, remote_file_name = os.path.split(destination_path)
ftp_client.cwd(remote_path)
ftp_client.storbinary(f'STOR {remote_file_name}', tmp_file)
The file is transferred, but i have en error like this : "SSLEOFError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:2702)"
That resulted from '/usr/lib/python3.10/ssl.py' (3rd line which says 'shutdown'):
def unwrap(self):
if self._sslobj:
s = self._sslobj.shutdown()
self._sslobj = None
return s
Does anyone have a solution or suggestion for this problem ? thanks.