Network Computing is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Python Networking: FTP Programming

This chapter explores the FTP, email, and CGI communications protocol with a Python recipe. Using Python, you can easily code simple FTP actions such as a file download and upload.

There are some interesting recipes in this chapter, such as manipulating your Google email, also known as the Gmail account, from your Python script. You can use these recipes to check, download, and send emails with IMAP, POP3, and SMTP protocols. In another recipe, a web server with CGI also demonstrates the basic CGI action, such as writing a guest comment form in your web application.

Listing the files in a remote FTP server

You would like to list the files available on the official Linux kernel's FTP site, ftp.kernel.org. You can select any other FTP site to try this recipe.

Getting ready

If you work on a production/enterprise FTP site with a user account, you need a username and password. However, in this instance, you don't need a username (and password) with the anonymous FTP server at the University of Edinburgh as you can log in anonymously.

How to do it...

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:

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:

 

How it works...

This recipe uses ftplib    to create an FTP client session with ftp.kernel.org. The test_ftp_connection()   function takes the FTP path, username, and email address for connecting to the FTP server.

An FTP client session can be created by calling the FTP()  function of ftplib      with the preceding connection's credentials. This returns a client handle, which then can be used to run the usual FTP commands, such as the command to change the working directory or cwd(). The dir()  method returns the directory listing.

It is a good idea to quit the FTP session by calling ftp.quit().

Common error

If you encounter the following error when you run the program, check whether you still can access the FTP_SERVER_URL, by pinging to it:

 

Usually, the preceding error means the FTP does not exist anymore, or is not open any more with the anonymous access.

If so, replace the URL with another FTP address that allows anonymous access.

You may verify that the FTP address is valid by trying to open it through your FTP client:

Give anonymous and your email address as the username and password when prompted. The outcome is depicted in the following screenshot:

(Click on image for larger view)

 

Uploading a local file to a remote FTP server

You would like to upload a file to an FTP server.

Getting ready

Let us set up a local FTP server. In Unix/Linux, you can install VSFTPD (Very Secure File Transfer Protocol Daemon) FTP Server using the following command:

$ sudo apt-get install vsftpd

On a Windows machine, you can install the FileZilla FTP server, which can be downloaded from https://filezilla-project.org/download.php?type=server. FileZilla can also be installed in Linux. For example, in Debian-based Linux distributions such as Ubuntu:

$ sudo apt-get install filezilla

You should create an FTP user account following the FTP server package's user manual. You would also like to upload a file to an FTP server. You can specify the server address, login credentials, and filename as the input argument of your script. You should create a local file called readme.txt with any text in it.

How to do it...

Using the following script, let's set up a local FTP server. Once you have installed the FTP Server such as VSFTPD or FileZilla, you can upload a file to the logged-in user's home directory. You can specify the server address, login credentials, and filename as the input argument of your script.

Listing 5.2 gives the FTP upload example as follows:

 

If you set up a local FTP server and run the following script, this script will log in to the FTP server and then will upload a file. If a filename argument is not supplied from the command line by default, it will upload the readme.txt file:

 

How it works...

In this recipe, we assume that a local FTP server is running. Alternatively, you can connect to a remote FTP server. The ftp_upload() method uses the FTP() function of Python's ftplib to create an FTP connection object. With the login() method, it logs in to the server.

After a successful login, the ftp object sends the STOR command with either the storlines() or storbinary() method. The first method is used for sending ASCII text files such as HTML or text files. The latter method is used for binary data such as zipped archive.

It's a good idea to wrap these FTP methods with try-catch error-handling blocks, which is not shown here for the sake of brevity.

This tutorial is an excerpt from "Python Network Programming Cookbook – Second Edition" by Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker and published by Packt. Use the code ORNCC50 at checkout to save 50% on the recommended ebook retail price until March 25.