Python FtpLib
The ftplib module is an FTP client.
Contents
Usage
from ftplib import FTP # connect conn = FTP('ftp.example.com') conn.login(user='example', passwd='secret') # list files conn.dir('folder1') # or conn.cwd('folder1') conn.dir() # download a file with open('destination.txt', 'wb') as f: ftp.retrbinary('RETR source.txt', f.write, blocksize=1024) # upload a file with open('source.txt', 'b') as f: ftp.storbinary('STOR source.txt', f) # close connection conn.quit()
Classes
Ftp
ftplib.FTP is the core client object for the module. See the example above.
Methods
The following methods are available as interfaces to established FTP commands:
Method |
Command |
cwd(dir) |
CWD |
delete(fn) |
DELE |
dir(dir) |
LIST |
mkd(dir) |
MKD |
mlsd(dir) |
MLSD |
nlst(dir) |
NLST |
pwd() |
PWD |
quit() |
QUIT |
rename(old, new) |
RNFR, RNTO |
rmd(dir) |
RMD |
size(fn) |
SIZE |
The following other methods are also exposed.
Method |
Meaning |
abort() |
try to abort an ongoing file transfer; leverages the ABOR command, which actually aborts the last FTP command |
close() |
see above example |
connect() |
if an ftplib.FTP object was not instantiated with a host, call this method like FTP.connect(host='ftp.example.com', port=21, timeout=300) |
getwelcome() |
return the server's welcome message |
login()' |
see above example |
ntransfercmd() |
|
retrbinary() |
see above example |
retrlines() |
|
sendcmd() |
send an FTP command and return the response as a string |
set_debuglevel() |
|
set_pasv() |
|
storbinary() |
see above example |
storlines() |
|
transfercmd() |
|
voidcmd() |
send an FTP command and raise ftplib.error_reply if a response code outside of 200-299 is received |
Ftp_Tls
A subclass of ftplib.FTP with additional methods and properties.
Method |
Meaning |
auth() |
upgrade to secure connection |
ccc() |
revert to plaintext connection |
prot_c() |
setup plaintext (clear) connection |
prot_p() |
setup secure (protected) connection |
Property |
Meaning |
ssl_version |
version of SSL/TLS to use (default: ssl.PROTOCOL_SSLv23) |
Exceptions
Exception |
Meaning |
ftplib.all_errors |
tuple of all following errors |
ftplib.error_reply |
unexpected response is received |
ftplib.error_temp |
temporary error (400-499) |
ftplib.error_perm |
permanent error (500-599) |
ftplib.error_proto |
improperly-formatted response is received |
See also
Python ftplib module documentation