Differences between revisions 1 and 6 (spanning 5 versions)
Revision 1 as of 2023-04-24 20:56:30
Size: 1314
Comment:
Revision 6 as of 2023-10-11 17:54:41
Size: 4450
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

# 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 ==
Line 17: Line 40:
See the example above.

The following methods are available as interfaces to established [[Protocols/FTP|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` ||

----
Line 21: Line 63:
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.
Line 27: Line 71:
Unilaterally close a connection with the `close()` method.

The 'correct' thing to do is to instead use `quit()` which implicitly closes the connection.
Line 33: Line 81:
----



==== Cwd ====

----



==== Delete ====

----



==== Dir ====
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)
}}}
Line 57: Line 95:
The '''`getwelcome()`''' method returns the server's welcome message. This is not guaranteed to be a machine-readable message, but may include useful information.
Line 63: Line 103:
----



==== Mkd ====

----



==== Mlsd ====

----



==== Nlst ====
The '''`login()`''' method authenticates with the server. See the above example.
Line 91: Line 115:
==== Pwd ====

----



==== Quit ====

----



==== Rename ====

----


Line 111: Line 117:
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)
}}}
Line 121: Line 136:
==== Rmd ====

----


Line 129: Line 138:
The '''`sendcmd()`''' method sends an FTP command and returns the response as a string.
Line 145: Line 156:
==== Size ====

----


Line 153: Line 158:
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)
}}}
Line 171: Line 185:
The '''`voidcmd()`''' method sends an FTP command. If a response outside of 200-299 is received, an `ftplib.error_reply` is raised.
Line 177: Line 193:


==== Auth ====

----



==== Ccc ====

----



==== Prot_P ====

----



==== Prot_C ====

----



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

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

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