If this is just a problem you'd like to solve, I might suggest the wget
command:
cd c:\destination
wget --mirror --continue --no-host-directories --user=username --password=s3cr3t ftp://hostname/source/path/
The --continue
option could be very dangerous if files change on the server. If files are only ever added, then it is very friendly.
However, if this is a learning exercise for you and you'd like to make your program work, I think you should start by looking at this line:
for subdir, dirs, files in os.walk(directory):
directory
has been the remote source directory in most of your program, but the os.walk()
function cannot walk a remote directory. You need to iterate over the returned files yourself, using a callback supplied to the retrlines
function.
Take a look at the MLSD
or NLST
options instead of LIST
, they will probably be easier to parse. (Note that FTP doesn't actually specify how lists should look; it was always intended to be driven by a human at a console, or a specific filename transferred. So programs that do clever things with FTP listings like present them to the user in a GUI probably have to have huge piles of special case code, for odd or obscure servers. And they probably all do something stupid when faced with malicious file names.)
Can you use sftp
instead? sftp
does have a specification for how file listings are supposed to be parsed, doesn't transmit username/password in the clear, and doesn't have the giant annoyance of passive vs active connections -- it simply uses the single connection, which means it works across more firewalls than FTP does.
Edit: You need to pass a 'callable' object to the retrlines
function. A callable object is either an instance of a class that defined a __call__
method, or a function. While the function might be easier to describe, an instance of a class may be more useful. (You could use the instance to collect the filenames, but the function would have to write to a global variable. Bad.)
Here's one of the simplest callable object:
>>> class c:
... def __call__(self, *args):
... print(args)
...
>>> f = c()
>>> f('hello')
('hello',)
>>> f('hello', 'world')
('hello', 'world')
This creates a new class, c
, that defines an instance method __call__
. This just prints its arguments in a fairly stupid manner, but it shows how minimal we're talking. :)
If you wanted something smarter, it could do something like this:
class handle_lines:
def __init__(self):
self.lines = []
def __call__(self, *args):
self.lines << args[0]
Call iterlines
with an object of this class, then look in the object's lines
member for details.
ftp.quit()
instead offtp.close()
. Please see this link – Tael