So, I'm struggling trying to upload a file to a remote ftps server that requires session reuse.
I must specify that even if I can connect to the server without problems via Filezilla, I get the error "unknown server certificate".
This is my script:
import ftplib
import ssl
USER = "username"
PASS = "password"
SERVER = "1.2.3.4"
PORT = 21
BINARY_STORE = True
filepath = '/path/to/myfile.txt'
filename = 'myfile.txt'
content = open(filepath, 'rb')
ftp = ftplib.FTP_TLS()
ftp.set_debuglevel(2)
print (ftp.connect(SERVER, PORT))
print (ftp.login(USER, PASS))
print (ftp.prot_p())
print (ftp.set_pasv(True))
print (ftp.cwd("testfolder"))
print (ftp.storbinary('STOR %s' % filename, content))
ftp.quit()
But I get this message: 450 TLS session of data connection has not resumed or the session does not match the control connection
The file is created on server but it's empty (0 bytes size) I've read dozen posts and I clearly understood that I must implement session reuse on client side too - which is supported from Python 3.6 - but simply... I don't know how to do this, since I can't find any working example.
Someone suggested to extend FTP_TLS class:
class MyFTP_TLS(ftplib.FTP_TLS):
"""Explicit FTPS, with shared TLS session"""
def ntransfercmd(self, cmd, rest=None):
conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
if self._prot_p:
session = self.sock.session
if isinstance(self.sock, ssl.SSLSocket):
session = self.sock.session
conn = self.context.wrap_socket(conn,
server_hostname=self.host,
session=session) # this is the fix
return conn, size
ftp = MyFTP_TLS()
...
But now the server response is: 'SSLSocket' object has no attribute 'session'
So I'm stuck. What am I missing?