I would like to check if a file exists in my FTP server. I have this function:
from ftplib import FTP_TLS
from configparser import ConfigParser
def check_ftp_file_exists(filename):
config = ConfigParser()
config.read("config.ini")
user = config["ftp"]["user"]
passwd = config["ftp"]["password"]
ftp = config["ftp"]["host"]
with FTP_TLS(ftp, user, passwd) as ftp:
file_list = ftp.nlst()
if filename in file_list:
return True
else:
return False
This function returns an error:
ftplib.error_perm: 522 Data connections must be encrypted.
. Here (How to upload a file on FTPS server using m2crypto) I found that I should use ftp.prot_p()
. But this function:
from ftplib import FTP_TLS
from configparser import ConfigParser
def check_ftp_file_exists(filename):
config = ConfigParser()
config.read("config.ini")
user = config["ftp"]["user"]
passwd = config["ftp"]["password"]
ftp = config["ftp"]["host"]
with FTP_TLS(ftp, user, passwd) as ftp:
ftp.prot_p()
file_list = ftp.nlst()
if filename in file_list:
return True
else:
return False
returns another error ftplib.error_perm: 522 SSL connection failed: session reuse required
. How can I fix it please?