Text of docs.python.org › 3 › › library › ftplib.html as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

ftplib — FTP protocol client — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » The Python standard library » Internet Protocols and Support » ftplib — FTP protocol client | Theme | ftplib — FTP protocol client ¶ Source code: Lib/ftplib.py This module defines the class FTP and a few related items. The FTP class implements the client side of the FTP protocol. You can use this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers.

It is also used by the module urllib.request to handle URLs that use FTP. For more information on FTP (File Transfer Protocol), see internet RFC 959 . The default encoding is UTF-8, following RFC 2640 . Availability : not WASI. This module does not work or is not available on WebAssembly. See WebAssembly platforms for more information. Here’s a sample session using the ftplib module: >>> from ftplib import FTP >>> ftp = FTP ( 'ftp.us.debian.org' ) # connect to host, default port >>> ftp . login () # user anonymous, passwd anonymous@ '230 Login successful.' >>> ftp . cwd ( 'debian' ) # change into "debian" directory '250 Directory successfully changed.' >>> ftp . retrlines ( 'LIST' ) # list directory contents -rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README ... drwxr-sr-x 5 1176 1176 4096 Dec 19 2000 pool drwxr-sr-x 4 1176 1176 4096 Nov 17 2008 project drwxr-xr-x 3 1176 1176 4096 Oct 10 2012 tools '226 Directory send OK.' >>> with open ( 'README' , 'wb' ) as fp : ... ftp . retrbinary ( 'RETR README' , fp . write ) '226 Transfer complete.' >>> ftp . quit () '221 Goodbye.' Reference ¶ FTP objects ¶ class ftplib.

FTP ( host = '' , user = '' , passwd = '' , acct = '' , timeout = None , source_address = None , * , encoding = 'utf-8' ) ¶ Return a new instance of the FTP class. Parameters : host ( str ) – The hostname to connect to. If given, connect(host) is implicitly called by the constructor. user ( str ) – The username to log in with (default: 'anonymous' ). If given, login(user, passwd, acct) is implicitly called by the constructor. passwd ( str ) – The password to use when logging in.

If not given, and if passwd is the empty string or "-" , a password will be automatically generated. acct ( str ) – Account information to be used for the ACCT FTP command. Few systems implement this. See RFC-959 for more details. timeout ( float | None ) – A timeout in seconds for blocking operations like connect() (default: the global default timeout setting). source_address ( tuple | None ) – A 2-tuple (host, port) for the socket to bind to as its source address before connecting. encoding ( str ) – The encoding for directories and filenames (default: 'utf-8' ).

The FTP class supports the with statement, e.g.: >>> from ftplib import FTP >>> with FTP ( "ftp1.at.proftpd.org" ) as ftp : ... ftp . login () ... ftp . dir () ... '230 Anonymous login ok, restrictions apply.' dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 . dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .. dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora >>> Changed in version 3.2: Support for the with statement was added.

Changed in version 3.3: source_address parameter was added. Changed in version 3.9: If the timeout parameter is set to be zero, it will raise a ValueError to prevent the creation of a non-blocking socket. The encoding parameter was added, and the default was changed from Latin-1 to UTF-8 to follow RFC 2640 . Several FTP methods are available in two flavors: one for handling text files and another for binary files. The methods are named for the command which is used followed by lines for the text version or binary for the binary version.

FTP instances have the following methods: set_debuglevel ( level ) ¶ Set the instance’s debugging level as an int . This controls the amount of debugging output printed. The debug levels are: 0 (default): No debug output. 1 : Produce a moderate amount of debug output, generally a single line per request. 2 or higher: Produce the maximum amount of debugging output, logging each line sent and received on the control connection. connect ( host = '' , port = 0 , timeout = None , source_address = None ) ¶ Connect to the given host and port.

This function should be called only once for each instance; it should not be called if a host argument was given when the FTP instance was created. All other FTP methods can only be called after a connection has successfully been made. Parameters : host ( str ) – The host to connect to. port ( int ) – The TCP port to connect to (default: 21 , as specified by the FTP protocol specification). It is rarely needed to specify a different port number. timeout ( float | None ) – A timeout in seconds for the connection attempt (default: the global default timeout setting). source_address ( tuple | None ) – A 2-tuple (host, port) for the socket to bind to as its source address before connecting.

Raises an auditing event ftplib.connect with arguments self , host , port . Changed in version 3.3: source_address parameter was added. getwelcome ( ) ¶ Return the welcome message sent by the server in reply to the initial connection. (This message sometimes contains disclaimers or help information that may be relevant to the user.) login ( user = 'anonymous' , passwd = '' , acct = '' ) ¶ Log on to the connected FTP server.

This function should be called only once for each instance, after a connection has been established; it should not be called if the host and user arguments were given when the FTP instance was created. Most FTP commands are only allowed after the client has logged in. Parameters : user ( str ) – The username to log in with (default: 'anonymous' ). passwd ( str ) – The password to use when logging in. If not given, and if passwd is the empty string or "-" , a password will be automatically generated. acct ( str ) – Account information to be used for the ACCT FTP command.

Few systems implement this. See RFC-959 for more details. abort ( ) ¶ Abort a file transfer that is in progress. Using this does not always work, but it’s worth a try. sendcmd ( cmd ) ¶ Send a simple command string to the server and return the response string. Raises an auditing event ftplib.sendcmd with arguments self , cmd . voidcmd ( cmd ) ¶ Send a simple command string to the server and handle the response. Return the response string if the response code corresponds to success (codes in the range 200–299).

Raise error_reply otherwise. Raises an auditing event ftplib.sendcmd with arguments self , cmd . retrbinary ( cmd , callback , blocksize = 8192 , rest = None ) ¶ Retrieve a file in binary transfer mode. Parameters : cmd ( str ) – An appropriate RETR command: "RETR filename " . callback ( callable ) – A single parameter callable that is called for each block of data received, with its single argument being the data as bytes . blocksize ( int ) – The maximum chunk size to read on the low-level socket object created to do the actual transfer.

This also corresponds to the largest size of data that will be passed to callback . Defaults to 8192 . rest ( int ) – A REST command to be sent to the server. See the documentation for the rest parameter of the transfercmd() method. retrlines ( cmd , callback = None ) ¶ Retrieve a file or directory listing in the encoding specified by the encoding parameter at initialization. cmd should be an appropriate RETR command (see retrbinary() ) or a command such as LIST or NLST (usually just the string 'LIST' ).

LIST retrieves a list of files and information about those files. NLST retrieves a list of file names. The callback function is called for each line with a string argument containing the line with the trailing CRLF stripped. The default callback prints the line to sys.stdout . set_pasv ( val ) ¶ Enable “passive” mode if val is true, otherwise disable passive mode. Passive mode is on by default. storbinary ( cmd , fp , blocksize = 8192 , callback = None , r

The rest of this page is on the live site.