These are the main ftplib functions we can use to execute ftp operations:
- FTP.getwelcome(): Gets the welcome message
- FTP.mkd(route): Creates a directory; it is passed as an input argument to the route
- FTP.rmd(path): Deletes the directory that we pass
- FTP.delete(file): Deletes the file that we passed as an input parameter
- FTP.pwd(): (Print Working Directory) Returns the current directory where it is located
- FTP.cwd(path): (Change Working Directory) Changes directory
- FTP.dir(path): Returns a list of directories
- FTP.nlst(path): Returns a list with the file names of the directory
- FTP.size(file): Returns the size of the file we passed to it
In this example, we are going to list the versions that are available in the Linux kernel FTP with the nlst() method.
You can find the following code in the list_kernel_versions_nslt.py file:
!/usr/bin/env python3
from ftplib import FTP
f = FTP('ftp.free.fr')
f.login()
f.cwd('/mirrors/ftp.kernel.org/linux/kernel/')
entries = f.nlst()
entries.sort()
print(len(entries), "entries:")
for entry in entries:
print(entry)
f.quit()