FTP Servers

Aidanmc
2 min readNov 29, 2020

There are three different types of FTP server connections. The different types are:

  • FTP
  • FTPS
  • FTPES

FTP: This is a plain unencrypted connection that by default connects over port 21. Most browsers support this type of connection.

FTPS: This type of connection works just like HTTPS. The connection is secured with SSL as soon as it set. The default port is 990. This was the first version of an encrypted FTP available, and while considered depreciated it is still widely used. No major web browser supports this type of connection.

FTPES: This type of connection starts out as a plain FTP connection over port 21, but through special commands is upgraded to SSL encryption. These commands usually occur before the user credentials are sent over. None of the major web browsers support this type of connection.

While there are multiple ways to set up an FTP server interacting with them all works roughly the same.

ftps = MyFTP_TLS(server, timeout=5)
ftps.login(user=user, passwd=password)
ftps.prot_p()
files = []
ftps.retrlines('LIST', files.append)
pprint(files)
def downloadFiles(path, destination):
try:
ftps.cwd(path)
os.chdir(destination)
os.makedirs(destination[0:len(destination)-1] + path)
except OSError:
pass
except ftplib.error_perm:
print ("Error: could not change to " + path)
sys.exit("Ending Application")
filelist=ftps.nlst() for file in filelist:
time.sleep(interval)
try:
with open(path + "/" + file, 'w+b') as f:
res = ftps.retrbinary('RETR %s' % file, f.write)
except ftplib.error_perm:
os.chdir(destination[0:len(destination)-1] + path)
try:
ftp.retrbinary("RETR " + file, open(os.path.join(destination + path, file),"wb").write)
print ("Downloaded: " + file)
except:
print ("Error: File could not be downloaded " + file)
return

This function will allow you to log into an FTPES server and download the files from the desired folder. To work with FTPES and FTPS the process is the same you just need to use a different library from Python. For FTP all you need to change is the library and not log in.

--

--