Use ftplib to build an FTP-CLIENT in Python

Tram Ho

FTP protocol : A fairly common protocol (before 2010), which is preferred by many programmers for file storage for sharing between systems.

While it may seem outdated at present, but there are still some “old” generations still in use and “who may be surprised”, one day we will have to work with it.

I have encountered such a case, the client’s system is Windows Server, their current plan to share files is to use FTP Server.

My task is to access the customer’s server, move to the exact folder they specified, search for any newly created files (pushed by the customer), download the file. The processor then accesses the ftp server and uploads the file to a directory they specify, sometimes creating a new directory to execute.

I solved the above problem using python’s ftplib library. Please see my partial listing code below.

Build a ftp server

To continue with the article, if you want to perform the demo according to my code, you can create your own ftp-server following the instructions.

To build an FTP Server, on Windows is very easy, use FileZilla Server:

For Ubuntu, follow the instructions (remember to select Authenticated):

Features of FTP Server:

  • File storage
  • User / password management
  • Assign access rights
  • Provide connection through any port (default: 21):
    • Connect anonymously.
    • Connection identifier: user / password.

FTP Client:

  • Program on the user’s computer, used to connect to the FTP Server

Building FTP-CLIENT using ftplib library

In Python, if you want to connect to FTP, use the ftplib library, this is the default library in the system (available after installing python) so no need to install more.

Initiate connection

After successful login, the data returns:

‘230 Logged on’

See ftp-server specifications

‘220-FileZilla Server 0.9.60 beta n220-written by Tim Kosse ( [email protected] ) n220 Please visit

The commands related to directory / file.

Display the current directory


Depending on the configuration & authorization of the server according to each login user, the default is to the ftp-server root directory, whose user after logging in will be assigned to a deep internal directory and does not have permission to translate. move out to external folders.

Lists directories / files in the directory connected to

Move to another folder from the parent directory:

Note: Only move up to 1 subfolder level

Move to the previous folder.

‘200 CDUP successful. “/” is current directory. ‘

Create a new folder

Delete directory

File related command

Check the file size

The unit of capacity here is Byte

Delete the file

Upload the file.

  • Conditions upload file from the client must be in the same or deeper than the location where the file runs ftp-client (file code .python) or must point the correct path to the file location.
  • Want to upload to a certain folder, then make a connection to that folder on the server.

Download the file

  • Where to download, run the program file at that location.
  • Connect to the FTP server and download

Close the connection.

Close the connection when done downloading / uploading.

<bound method ftp_client.close of <ftplib.FTP object at 0x000001F61AFE65F8 >>

In addition to the basic commands above, you can refer to the other commands of ftplib from the following link:

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo