ska_ftp

Light wrapper around Python ftplib to make it easier to use. Just adds a few convenience methods to ftplib.FTP class: ls(), ls_full(), put(), get(), and cd(). Also supports using a .netrc file which is parsed internally.

Example

import os
import ska_ftp

lucky = ska_ftp.FTP('lucky')

lucky.cd('/taldcroft')
print lucky.ls()

local_filename = os.path.join(os.environ['HOME'], '.cshrc')
lucky.put(local_filename, '/taldcroft/remote_cshrc')
lucky.get('remote_cshrc')
lucky.delete('remote_cshrc')
lucky.close()

orig = open('remote_cshrc').read()
roundtrip = open(local_filename).read()
if orig != roundtrip:
    print "File corruption during round trip to FTP server"
os.remove('remote_cshrc')

Netrc file

The user netrc file (typically ~/.netrc) can be used to determine the username and password for a particular FTP server. This file must be set to be readable only by the user for a minimal level of security, e.g.

chmod 600 ~/.netrc

An example ~/.netrc file is:

machine  lucky.cfa.harvard.edu
login    taldcroft
password not_mY_pass1word

Classes

class ska_ftp.ftp.FTP(host, user=None, passwd=None, netrcfile=None, logger=None)[source]

Initialize object for simpler ftp operations.

The FTP object has an attribute ftp which is the actual ftplib.FTP() object. This can be used for operations not supported by this class.

Parameters:
  • host – ftp host name

  • user – user name (default=netrc value or anonymous)

  • passwd – password (default=netrc value or anonymous@ )

  • netrcfile – netrc file name (default=~/.netrc)

  • logger – logger object (e.g. pyyaks.logger.get_logger())

cd(dirname)[source]

Change to specified directory dirname.

Parameters:

dirname – directory name

get(remotefile, localfile=None)[source]

Get the remotefile from the FTP server as localfile on the local host.

Parameters:
  • remotefile – file name on remote FTP host

  • localfile – file name on local host (default=remotefile)

ls(dirname='', *args)[source]

List contents of directory dirname via NLST command.

Parameters:

dirname – directory name

Returns:

list of file and/or directory names

ls_full(dirname='', *args)[source]

List full contents of directory dirname.

Parameters:

dirname – directory name

Returns:

list of full FTP output for LIST command

put(localfile, remotefile=None)[source]

Put the localfile to the FTP server as remotefile.

Parameters:
  • localfile – file name on local host

  • remotefile – file name on remote FTP host (default=localfile)

Functions

ska_ftp.ftp.parse_netrc(netrcfile=None)[source]

Get default user and password for an FTP server by parsing a .netrc file.

The returned object is a dict with a key for each machine defined in the netrc file. The corresponding value is another dict containing all the key/value pairs defined for that machine.

Note that the default token in the .netrc file is not parsed by this routine. See man netrc for information on the format of this file.

Parameters:

netrcfile – name of the netrc file (default=~/.netrc)

Returns:

dict of configuration dicts