Python FtpLib

The ftplib module is an FTP client.


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()

# close connection
conn.quit()


Classes

Ftp

See the example above.

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


Abort

The abort() method tries to abort an ongoing file transfer. This leverages the ABOR command, which actually aborts the last FTP command. This method won't always work.


Close

Unilaterally close a connection with the close() method.

The 'correct' thing to do is to instead use quit() which implicitly closes the connection.


Connect

If an ftplib.FTP object was not instantiated with a host, call the connect method to connect to a server.

ftp = ftplib.FTP()

ftp.connect(host='ftp.example.com',  port=21, timeout=300)


GetWelcome

The getwelcome() method returns the server's welcome message. This is not guaranteed to be a machine-readable message, but may include useful information.


Login

The login() method authenticates with the server. See the above example.


NTransferCmd


RetrBinary

The retrbinary method is a low-level file download interface. The first argument should be a valid RETR command. The second argument should be a callback that can handle binary data received as a response. If a blocksize is not specified, 8192 is the default.

Try:

with open('destination.txt', 'wb') as f:
    ftp.retrbinary('RETR source.txt', f.write, blocksize=1024)


RetrLines


SendCmd

The sendcmd() method sends an FTP command and returns the response as a string.


Set_DebugLevel


Set_Pasv


StorBinary

The storbinary method is a low-level file download interface. The first argument should be a valid STOR command. The second argument is a binary file object. If a blocksize is not specified, 8192 is the default.

Try:

with open('source.txt', 'b') as f:
    ftp.storbinary('STOR source.txt', f)


StorLines


TransferCmd


VoidCmd

The voidcmd() method sends an FTP command. If a response outside of 200-299 is received, an ftplib.error_reply is raised.


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


CategoryRicottone