python logo

python ftp client


Python hosting: Host, run, and code Python in the cloud!

FTP or File Transfer Protocol is an essential protocol for transferring files across different systems over a TCP network. Introduced in 1971, FTP has become a widely recognized method to share large-sized files over the internet. In Python, one can use the FTP protocol efficiently using the ftplib library.

Understanding the Basics
The ftplib library in Python provides a plethora of functions that facilitate the creation and access of remote files. Whether it’s listing directory contents or uploading files, the functions available provide a streamlined approach.

Listing Directories
The following Python snippet showcases how one can list the contents of the root directory:

import ftplib

ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

data = []

ftp.dir(data.append)

ftp.quit()

for line in data:
print "-", line

If you have a specific directory in mind, then post-connection, utilize the ftp.cwd(‘/‘) function and specify the desired directory:

import ftplib

ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

data = []

ftp.cwd('/pub/') # change directory to /pub/
ftp.dir(data.append)

ftp.quit()

for line in data:
print "-", line

Downloading Files
The retrbinary() function comes in handy when downloading files. Check out the example below for a clearer understanding:

import ftplib
import sys

def getFile(ftp, filename):
try:
ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
except:
print "Error"

ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")

ftp.cwd('/pub/') # change directory to /pub/
getFile(ftp,'README.nluug')

ftp.quit()

Uploading Files
For uploading files, the storlines() function is your best bet. The example below demonstrates the upload of the README.nluug file into the main directory:

import ftplib
import os

def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

ftp = ftplib.FTP("127.0.0.1")
ftp.login("username", "password")

upload(ftp, "README.nluug")

Additional ftplib Functions
The ftplib library encompasses numerous functions that haven’t been covered here. For an exhaustive list and in-depth details, consult the official library documentation.

Further Learning
If you wish to delve deeper into Python programming and gain a robust understanding, consider enrolling in the Python Programming Bootcamp: Go from zero to hero.

← Previous Tutorial
Next Tutorial →






Leave a Reply:




Andy Sun, 4 Apr 2021

And the synchronisation of data is automatically during all the opening script?

Frank Sun, 4 Apr 2021

No, it's just an FTP client (file transfer protocol). You can get and put files to an FTP server, but the files are not synchronous. The protocol itself does not support synchronisation. You are looking for something like async instead. The *ftplib* module lets you connect to any FTP server and upload and download files.

To be more elaborate, The FTP protocol is part of the TCP/IP protocol suite and is a means of transfering files from one host to another over the Internet, either public or private. It is an elegant but very simple protocol, and many libraries are available for programming languages. The Python standard library includes a module ftplib which implements the FTP protocol in Python. Python supports the ftplib module.

The module provides two classes for FTP: The ftp.FTP() class is a subclass of socket.socket() and defines an FTP connection but should be used only for communications with passive FTP servers. The ftplib.FTP() class defines a complete FTP client and is used to communicate with both active and passive FTP servers.