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.
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')
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
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: |
|
---|
Change to specified directory dirname.
Parameters: | dirname – directory name |
---|
Get the remotefile from the FTP server as localfile on the local host.
Parameters: |
|
---|
List contents of directory dirname via NLST command.
Parameters: | dirname – directory name |
---|---|
Returns: | list of file and/or directory names |
List full contents of directory dirname.
Parameters: | dirname – directory name |
---|---|
Returns: | list of full FTP output for LIST command |
Put the localfile to the FTP server as remotefile.
Parameters: |
|
---|
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 |