We can use the ftplib library to fetch files from our selected FTP site. A detailed documentation of this library can be found at https://docs.python.org/3/library/ftplib.html for Python 3 and at http://docs.python.org/2/library/ftplib.html for Python 2.
ftplib is a built-in Python module, and you do not need to install it separately. Let us see how we can fetch some files with ftplib.
Listing 5.1 gives a simple FTP connection test as follows:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 5 # This program is optimized for Python 2.7.12 and Python 3.5.2. # It may run on any other version with/without modifications. FTP_SERVER_URL = 'ftp.ed.ac.uk' import ftplib from ftplib import FTP def test_ftp_connection(path, username, email): #Open ftp connection ftp = ftplib.FTP(path, username, email) #List the files in the /pub directory ftp.cwd("/pub") print ("File list at %s:" %path) files = ftp.dir() print (files) ftp.quit() if __name__ == '__main__': test_ftp_connection(path=FTP_SERVER_URL, username='anonymous', email='[email protected]', )
This recipe will list the files and folders present in the FTP path, ftp.kernel.org/pub. If you run this script, you can see the following output:
$ python 15_1_list_files_on_ftp_server.py File list at ftp.ed.ac.uk: drwxr-xr-x 4 1005 bin 4096 Oct 11 1999 EMWAC drwxr-xr-x 13 31763 netserv 4096 May 5 2010 EdLAN drwxrwxr-x 4 6267 6268 4096 Oct 11 1999 ITFMP drwxr-xr-x 4 1407 bin 4096 Oct 11 1999 JIPS drwxr-xr-x 2 root bin 4096 Oct 11 1999 Mac drwxr-xr-x 2 10420 7525 4096 Oct 7 2003 PaedTBI drwxr-xr-x 2 jaw bin 4096 Oct 11 1999 Printing drwxr-xr-x 3 root bin 4096 Oct 11 1999 Student_Societies drwxr-xr-x 6 root bin 4096 Feb 19 2014 Unix drwxr-xr-x 2 root bin 4096 Oct 11 2016 Unix1 drwxr-xr-x 2 1109 bin 4096 Oct 19 2016 Unix2 drwxr-xr-x 2 2022 bin 4096 Oct 11 1999 X.400 drwxr-xr-x 2 20076 bin 4096 Feb 17 2000 atoz drwxr-xr-x 2 1403 bin 4096 Aug 9 2001 bill drwxr-xr-x 2 4414 bin 4096 Oct 11 1999 cartonet drwxr-xr-x 2 1115 bin 4096 Oct 11 1999 courses drwxr-xr-x 2 10498 bin 4096 Oct 11 1999 esit04 drwxr-xr-x 2 6314 bin 4096 Oct 11 1999 flp drwxr-xr-x 2 1400 bin 4096 Nov 19 1999 george drwxr-xr-x 3 309643 root 4096 Sep 10 2008 geos drwxr-xr-x 2 1663 root 4096 Apr 23 2013 hssweb drwxr-xr-x 2 6251 bin 4096 Oct 11 1999 ierm drwxr-xr-x 2 2126 bin 4096 Nov 12 2004 jbm drwxr-xr-x 2 1115 bin 4096 Oct 11 1999 kusch drwxr-xr-x 2 root bin 4096 Oct 11 1999 lrtt drwxr-xr-x 7 scott bin 4096 Nov 9 2015 mail drwxr-xr-x 3 1407 bin 4096 Oct 11 1999 maps drwxr-xr-x 2 2009 bin 4096 Oct 11 1999 mmaccess drwxr-xr-x 2 2009 bin 4096 Oct 11 1999 mmsurvey drwx--x--x 3 1943 bin 4096 Dec 1 2000 mww drwxr-xr-x 2 root bin 4096 Oct 11 1999 pbowers drwxr-xr-x 5 7324 bin 4096 Oct 11 1999 rip99 drwxr-xr-x 11 2223 bin 4096 Sep 30 2011 soroti drwxr-xr-x 2 root bin 4096 Oct 6 2000 steve drwxr-xr-x 2 2000 bin 4096 Oct 11 1999 ucsg drwxr-xr-x 7 20099 bin 4096 Jul 28 2003 unixhelp drwxr-xr-x 2 root bin 4096 Oct 11 1999 utopia drwxr-xr-x 2 2022 bin 4096 Oct 11 1999 whiteosi None