#! /usr/bin/env python """ Python program that demonstrates portability issues with FTPUTIL. """ import ftputil import ftplib import sys import time # download some files from the login directory my_host = 'ftp.python.org' my_user = 'anonymous' my_password = 'rsg@TeamSTARS.net' directory_1 = 'pub' directory_2 = 'python' directory_3 = '2.4.1' tarball = 'Python-2.4.1.tgz' tarball_ftplib = tarball + '.ftplib' tarball_ftputil = tarball + '.ftputil' # in ftputil: ftputil.ftputil.ftplib.FTP.set_pasiv(True) def use_ftplib(): host = ftplib.FTP(my_host) # connect to host, default port host.set_debuglevel(0) host.set_pasv(False) host.getwelcome() host.login(my_user, my_password) # user anonymous, passwd anonymous@ # host.retrlines('LIST') # list directory contents i = 0 print 'ftplib #%d - %s' % (i, host.sendcmd('pwd')) host.retrlines('LIST') # list directory contents host.cwd(directory_1) i = i + 1 print 'ftplib #%d - %s' % (i, host.sendcmd('pwd')) host.retrlines('LIST') # list directory contents host.cwd(directory_2) i = i + 1 print 'ftplib #%d - %s' % (i, host.sendcmd('pwd')) host.retrlines('LIST') # list directory contents host.cwd(directory_3) i = i + 1 cmd = 'RETR %s' % tarball host.retrbinary(cmd, open(tarball_ftplib, 'wb').write) host.quit() return 0 def use_ftputil(): class ActiveFTPSession(ftplib.FTP): def __init__(self, host, userid, password): """Act like ftplib.FTP's constructor but use active mode explicitly.""" port = 21 ftplib.FTP.__init__(self) self.connect(host, port) print self.getwelcome() self.login(userid, password) self.set_debuglevel(0) self.set_pasv(False) my_login_path = self.sendcmd('pwd') print "\tmy_login_path for ActiveFTPSession: %s" % my_login_path names = self.retrlines('LIST') print "\tnames: %s" % names # see http://docs.python.org/lib/ftp-objects.html # host = ftputil.FTPHost('ftp.domain.com', 'user', 'password') host = ftputil.FTPHost(my_host, my_user, my_password, session_factory=ActiveFTPSession) ftputil_login_path = host.getcwd() print "\tftputil_login_path: %s" % ftputil_login_path names = host.listdir(host.curdir) print "\tnames: %s" % names host.chdir(directory_1) ftputil_login_path = host.getcwd() print "ftputil_login_path: %s" % ftputil_login_path names = host.listdir(ftputil_login_path) host.chdir(directory_2) ftputil_login_path = host.getcwd() print "ftputil_login_path: %s" % ftputil_login_path names = host.listdir(ftputil_login_path) host.chdir(directory_3) ftputil_login_path = host.getcwd() print "ftputil_login_path: %s" % ftputil_login_path names = host.listdir(ftputil_login_path) print "names: %s" % names for name in names: if name == tarball and host.path.isfile(name): print "name: %s" % name host.download(name, tarball_ftputil, 'b') # remote, local, binary mode # make a new directory and copy a remote file into it return 0 ##################################### if __name__ == '__main__': print ' Begin FTP-example Main %s' % (time.ctime(),) exitStatus = use_ftplib() exitStatus = use_ftputil() print ' ExitStatus: [ %s ]' % exitStatus print ' End FTP-example Main %s' % (time.ctime(),) sys.exit(exitStatus)