Differences between revisions 1 and 8 (spanning 7 versions)
Revision 1 as of 2023-04-24 20:56:30
Size: 1314
Comment:
Revision 8 as of 2023-10-11 20:13:45
Size: 5216
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
'''`ftplib`''' is a client module for FTP servers. The '''`ftplib`''' module is an [[Protocols/FTP|FTP]] client.
Line 13: Line 13:
{{{
from ftplib import FTP
Line 14: Line 16:
# connect
conn = FTP('ftp.example.com')
conn.login(user='example', passwd='secret')
Line 15: Line 20:
=== Ftp === # list files
conn.dir('folder1')
# or
conn.cwd('folder1')
conn.dir()
Line 17: Line 26:
# download a file
with open('destination.txt', 'wb') as f:
    ftp.retrbinary('RETR source.txt', f.write, blocksize=1024)
Line 18: Line 30:
# upload a file
with open('source.txt', 'b') as f:
    ftp.storbinary('STOR source.txt', f)
Line 19: Line 34:
==== Abort ==== # close connection
conn.quit()
}}}
Line 25: Line 42:
==== Close ====

----
== Classes ==
Line 31: Line 46:
==== Connect ==== === Ftp ===
Line 33: Line 48:
---- '''`ftplib.FTP`''' is the core client object for the module. See the example above.
Line 37: Line 52:
==== Cwd ==== ==== Methods ====
Line 39: Line 54:
---- The following methods are available as interfaces to established [[Protocols/FTP|FTP]] commands:
Line 41: Line 56:
||'''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` ||
Line 42: Line 69:
The following other methods are also exposed.
Line 43: Line 71:
==== Delete ====

----



==== Dir ====

----



==== GetWelcome ====

----



==== Login ====

----



==== Mkd ====

----



==== Mlsd ====

----



==== Nlst ====

----



==== NTransferCmd ====

----



==== Pwd ====

----



==== Quit ====

----



==== Rename ====

----



==== RetrBinary ====

----



==== RetrLines ====

----



==== Rmd ====

----



==== SendCmd ====

----



==== Set_DebugLevel ====

----



==== Set_Pasv ====

----



==== Size ====

----



==== StorBinary ====

----



==== StorLines ====

----



==== TransferCmd ====

----



==== VoidCmd ====
||'''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 ||
Line 177: Line 94:
A subclass of `ftplib.FTP` with additional methods and properties.
Line 178: Line 96:
||'''Method'''||'''Meaning''' ||
||`auth()` ||upgrade to secure connection ||
||`ccc()` ||revert to plaintext connection ||
||`prot_c()` ||setup plaintext (''clear'') connection ||
||`prot_p()` ||setup secure (''protected'') connection||
Line 179: Line 102:
==== Auth ==== ||'''Property'''||'''Meaning''' ||
||`ssl_version` ||version of SSL/TLS to use (default: `ssl.PROTOCOL_SSLv23`)||
Line 185: Line 109:
==== Ccc ==== == Exceptions ==
Line 187: Line 111:
----



==== Prot_P ====

----



==== Prot_C ====

----



==== Ssl_Version ====
||'''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||

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

# 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


CategoryRicottone

Python/FtpLib (last edited 2023-10-11 20:13:45 by DominicRicottone)