Introduction to FTP and ftplib

Let’s discuss File Transfer Protocol (FTP) and how it can be connected to the server, and how we can navigate directories with ftplib.

Overview

The File Transfer Protocol (FTP) is used by many companies and organizations for sharing data. Python provides a File Transfer Protocol module in its standard library called ftplib that implements the client side of the FTP protocol. We can learn all about the File Transfer Protocol by reading the RFC 959 document on the Internet. However the full specification is outside the scope of this chapter. Instead we will focus on the following topics:

  • Connecting to an FTP server
  • Navigating its structure
  • Downloading files from the FTP server
  • Uploading files to an FTP server

Let’s get started!

Connecting to an FTP server

The first thing we need to do is find an FTP server to connect to. There are many free ones we can use. For example, most Linux distributions have FTP mirrors that are publicly accessible. They aren’t just FTP though, so be sure that you choose the correct protocol or you will receive a connection error.

Get hands-on with 1400+ tech skills courses.