1. Home
  2. Computing & Technology
  3. Python

Building an FTP Client in Python

From , former About.com Guide

3 of 6

Initiating the Connection

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 requires. 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.
Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.