# Ftptest. py-an example application using Python's ftplib module.
# Author: Matt Croydon <Matt@ooiio.com>, Referencing multiple sources, including:
# Pydoc for ftplib:Http://web.pydoc.org/2.2/ftplib.html
# Ftplib module docs:Http://www.python.org/doc/current/lib/module-ftplib.html
# Python Tutorial:Http://www.python.org/doc/current/tut/tut.html
# License: gnu gpl. The software is free, don't sue me.
# This was written under Python 2.2, though it shoshould 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 ):
File. Write (Block)
Print ".",
# Create an instance of the FTP object
# Optionally, you cocould specify username and password:
# Ftp ('hostname', 'username', 'Password ')
FTP = FTP ('ftp .cdrom.com ')
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
PrintFTP. Login()
# This is the directory that we want to go
Directory = 'pub/simtelnet/trumpet/Winsock'
# Let's change to that directory. You kids might call these 'folders'
Print 'Changing to '+ directory
FTP. CWD (directory)
# Print the contents of the Directory
FTP. retrlines ('LIST')
# Here's a file for us to play with. Remember trumpet Winsock?
Filename = 'winap 21f . Zip'
# 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
File. Close ()
Print 'closing FTP connection'
PrintFTP. Close()