ftplib ftp objectpassing in the string name (domain or ip styleof the machine you wish to connect toconnection ftp(sitenameconnect to ftp site assuming this call doesn' throw an exceptionthe resulting ftp object exports methods that correspond to the usual ftp operations in factpython scripts act much like typical ftp client programs--just replace commands you would normally type or select with method callsconnection login(*userinfoconnection cwd(dirnamedefault is anonymous login xfer at time to localfile once connectedwe log in and change to the remote directory from which we want to fetch file the login method allows us to pass in username and password as additional optional arguments to specify an account loginby defaultit performs anonymous ftp notice the use of the nonpassive flag in this scriptif nonpassiveconnection set_pasv(falseforce active ftp if server requires if this flag is set to truethe script will transfer the file in active ftp mode rather than the default passive mode we'll finesse the details of the difference here (it has to do with which end of the dialog chooses port numbers for the transfer)but if you have trouble doing transfers with any of the ftp scripts in this try using active mode as first step in python and laterpassive ftp mode is on by default nowopen local file to receive the file' contentand fetch the filelocalfile open(filename'wb'connection retrbinary('retr filenamelocalfile write once we're in the target remote directorywe simply call the retrbinary method to download the target server file in binary mode the retrbinary call will take while to completesince it must download big file it gets three argumentsan ftp command stringherethe string retr filenamewhich is the standard format for ftp retrievals function or method to which python passes each chunk of the downloaded file' bytesherethe write method of newly created and opened local file size for those chunks of byteshere , bytes are downloaded at timebut the default is reasonable if this argument is omitted because this script creates local file named localfile of the same name as the remote file being fetchedand passes its write method to the ftp retrieval methodthe remote file' contents will automatically appear in localclient-side file after the download is finished observe how this file is opened in wb binary output mode if this script is run on windows we want to avoid automatically expanding any \ bytes into \ \ byte sequences client-side scripting