Python FTP With Examples

Today we will learn about python ftp operations. FTP is an acronym for File Transfer Protocol. Just like what it sounds, this protocol is used to transfer files across a network from source to destination machine.

In this post on python ftp module, we will see how to connect to a FTP server, upload & download files and much more. Let’s get started.

Python ftp

With Python, ftplib module provides all the functions which we might need to perform actions across the FTP protocol. Let’s start exploring this lesson with simple connection attempt.

Connecting to an FTP server with login() function

We will start by making a connection to an FTP server. We will use a server publicly available for usage, ftp.cse.buffalo.edu. Feel free to choose any server, just make sure you select the right server otherwise you will face connection errors.

We will write a simple script to connect to the said server:

See how we can login without a username and a password? That is supported by few servers only. Let us see the output:
ftplib-download
In this example, we started by importing a single class from complete module called FTP. We used its object to connect to a host and anonymously login to the server.
Of course, we didn’t pass a port to connect. This means just like any other request that this will arrive at the default port of the server. To change this, specify the port for the connection as well:

Do take care that the port you’re connecting to actually supports an incoming FTP connection.

Feel welcomed with getwelcome() function

A server can make you feel welcomed by returning a message. This message can be obtained using getwelcome() function. Easy to use, let’s put it in an example:

Output for this script will just show a simple message which can be anything:

ftp-getwelcome-1

Present Directory with pwd() function

We can get the path of presently working directory of the server our connection is present at currently:

Output for this script will just show present working directory:

ftp-getwelcome-1

Accessing Directories with retrlines() function

Now, just like an open SSH shell, we can use ftplib to access the directories on the server, navigate through them and change them based on needs.

Look at the last line, we used the retrlines(...) function to list the current directories. Output for this script will be:

ftplib-directories-1

Changing Directories with cwd() function

In above example, we listed the directories our script was currently accessing. To add, we can see current directory easily as:

Here, we’re switching to one of the sub-directories we noticed in an earlier run and we get the following results:
ftplib-download
The LIST attribute passed just pulls out the files and folders in current directory along with their information, which we then print.

Send command to server with sendcmd() function

Using the sendcmd() function, we can send a simple String command to the server and obtain the String response. For an example, we will send a command STAT which can check the status fo a server:

When we run this script, we see the complete output on our own console:

ftplib-sendcmd-1

Downloading files with retrbinary() function

Using ftpliob module, we can even download files locally. It is worth noticing that to do this, you must have proper access to a server and directories and file names and exact paths should be known.

We will simply access a file on server and download it locally:

I add some print statements in between to make the output a bit more clear. Let’s run this program now:
ftplib-download
And here is the file which was downloaded:
downloaded-file-1
Please note that you will need to modify the location of file download based on your local machine path before running this code.

Close connection with close() function

We should close the ftp connection once we’re done with any tasks needed to do be done:

Output for this script will just show present working directory:
ftplib-close-1
Remember, we cannot open a closed python ftp connection again.

In this lesson, we learned about various ways through which we can access an FTP server and play with directories and manage it completely.

Reference: API Doc

By admin

Leave a Reply