Initiating the connection is almost as straightforward as running an
FTP client manually. As you might expect, we need to give Python the
site for the connection and any login details which the server
As part of telling Python the site for the connection, we also create
an FTP object.
ftp = ftplib.FTP('ftp.myftpsite.com')
In this case, I have chosen the variable name 'ftp'. As long as 'ftp'
remains in lowercase or is only capitalised (e.g., 'Ftp'), there is
little chance of our confusing it with the class.
Here we have created an instance of ftplib's FTP class. That class
requires, at the least, the server's URL as an initial value and
returns a socket object which we then assign to a variable. If we
wanted to, we could here pass the login details and account name. The
syntax for that would be:
handle = ftplib.FTP(host, username, password, account_name)
For purposes of illustration, we will login separately.