utils.ftp module

FTP manipulation library

@author: Milan Falešník <mfalesni@redhat.com>

class utils.ftp.FTPClient(host, login, password)[source]

Bases: object

FTP Client encapsulation

This class provides basic encapsulation around ftplib’s FTP class. It wraps some methods and allows to easily delete whole directory or walk through the directory tree.


>>> from utils.ftp import FTPClient
>>> ftp = FTPClient("host", "user", "password")
>>> only_files_with_EVM_in_name = ftp.filesystem.search("EVM", directories=False)
>>> only_files_by_regexp = ftp.filesystem.search(re.compile("regexp"), directories=False)
>>> some_directory = ftp.filesystem.cd("a/b/c") # cd's to this directory
>>> root = some_directory.cd("/")

Always going through filesystem property is a bit slow as it parses the structure on each use. If you are sure that the structure will remain intact between uses, you can do as follows to save the time:

>>> fs = ftp.filesystem

Let’s download some files:

>>> for f in ftp.filesystem.search("IMPORTANT_FILE", directories=False):
...     f.download()    # To pickup its original name
...     f.download("custom_name")

We finished the testing, so we don’t need the content of the directory:

>>> ftp.recursively_delete()

And it’s gone.


Goes one level up in directory hierarchy (cd ..)


Finish work and close connection


Enter a directory

Parameters:d – Directory name
Returns:Success of the action

Remove a file

Parameters:f – File name
Returns:Success of the action

Returns the object structure of the filesystem

Returns:Root directory

Lists the content of a directory.

Returns:List of all items in current directory Return format is [(is_dir?, “name”, remote_time), ...]

Create a directory

Parameters:d – Directory name
Returns:Success of the action

Get current directory

Returns:Current directory
Raises:AssertionError – PWD command fails

Recursively deletes content of pwd

WARNING: Destructive!

  • d – Directory to enter (None for not entering - root directory)
  • d – str or None

AssertionError – When some of the FTP commands fail.

retrbinary(f, callback)[source]

Download file

You need to specify the callback function, which accepts one parameter (data), to be processed.

  • f – Requested file name
  • callback – Callable with one parameter accepting the data

Remove a directory

Parameters:d – Directory name
Returns:Success of the action
storbinary(f, file_obj)[source]

Store file

You need to specify the file object.

  • f – Requested file name
  • file_obj – File object to be stored

Walks the tree recursively and creates a tree

Base structure is a list. List contains directory content and the type decides whether it’s a directory or a file: - tuple: it’s a file, therefore it represents file’s name and time - dict: it’s a directory. Then the dict structure is as follows:

dir: directory name
content: list of directory content (recurse)
Parameters:d – Directory to enter(None for no entering - root directory)
Returns:Directory structure in lists nad dicts.
Raises:AssertionError – When some of the FTP commands fail.

Determine the time difference between the FTP server and this computer.

This is done by uploading a fake file, reading its time and deleting it. Then the self.dt variable captures the time you need to ADD to the remote time or SUBTRACT from local time.

The FTPFile object carries this automatically as it has .local_time property which adds the client’s .dt to its time.

class utils.ftp.FTPDirectory(client, name, items, parent_dir=None, time=None)[source]

Bases: object

FTP FS Directory encapsulation

This class represents one directory. Contains pointers to all child directories (self.directories) and also all files in current directory (self.files)


Change to a directory

Changes directory to a path specified by parameter path. There are three special cases: / - climbs by self.parent_dir up in the hierarchy until it reaches root element. . - does nothing .. - climbs one level up in hierarchy, if present, otherwise does the same as preceeding.

Parameters:path – Path to change

Returns – whole path for this directory

search(by, files=True, directories=True)[source]

Recursive search by string or regexp.

Searches throughout all the filesystem structure from top till the bottom until it finds required files or dirctories. You can specify either plain string or regexp. String search does classic in, regexp matching is done by exact matching (by.match).

  • by – Search string or regexp
  • files – Whether look for files
  • directories – Whether look for directories

List of all objects found in FS

exception utils.ftp.FTPException[source]

Bases: exceptions.Exception

class utils.ftp.FTPFile(client, name, parent_dir, time)[source]

Bases: object

FTP FS File encapsulation

This class represents one file in the FS hierarchy. It encapsulates mainly its position in FS and adds the possibility of downloading the file.


Download file into this machine

Wrapper around self.retr function. It downloads the file from remote filesystem into local filesystem. Name is either preserved original, or can be changed.

Parameters:target – Target file name (None to preserver the original)

Returns – time modified to match local computer’s time zone


Returns – whole path for this file


Retrieve file

Wrapper around ftplib.FTP.retrbinary(). This function cd’s to the directory where this file is present, then calls the FTP’s retrbinary() function with provided callable and then cd’s back where it started to keep it consistent.


callback – Any callable that accepts one parameter as the data

  • AssertionError – When any of the CWD or CDUP commands fail.
  • ftplib.error_perm – When retrbinary call of ftplib fails