Beyond the Basic Python Ftplib Example

I've been frustrated at some of the documentation and tutorials about Python.  I was looking for something beyond your typical ftplib tutorial/example/howto.  I couldn't find any, so I put together this python script that will connect to a server, print directory information, download a file using retrbinary and a callback function (which I wasn't able to find in any examples), and other various fun stuff.  There's lots more to be done, but I think that this is a slightly advanced start.  Please refer to the resources linked to below and in the file for details.

The python source is also available under terms of the GNU GPL:

# - An example application using Python's ftplib module.
# Author: Matt Croydon <>, referencing many sources, including:
#   Pydoc for ftplib:
#   ftplib module docs:
#   Python Tutorial:
# License: GNU GPL.  The software is free, don't sue me.
# This was written under Python 2.2, though it should work with Python 2.x and greater.

# Import the FTP object from ftplib
from ftplib import FTP

# This will handle the data being downloaded
# It will be explained shortly
def handleDownload(block):
    print ".",
# Create an instance of the FTP object
# Optionally, you could specify username and password:
# FTP('hostname', 'username', 'password')
ftp = FTP('')

print 'Welcome to Matt's ftplib example'
# Log in to the server
print 'Logging in.'
# You can specify username and password here if you like:
ftp.login('username', 'password')
# Otherwise, it defaults to Anonymous

# This is the directory that we want to go to
directory = 'pub/simtelnet/trumpet/winsock'
# Let's change to that directory.  You kids might call these 'folders'
print 'Changing to ' + directory

# Print the contents of the directory

# Here's a file for us to play with.  Remember Trumpet Winsock?
filename = ''

# Open the file for writing in binary mode
print 'Opening local file ' + filename
file = open(filename, 'wb')

# Download the file a chunk at a time
# Each chunk is sent to handleDownload
# We append the chunk to the file and then print a '.' for progress
# RETR is an FTP command
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload)

# Clean up time
print 'Closing file ' + filename

print 'Closing FTP connection'