:mod:`!ftplib` --- FTP protocol client
.. module:: ftplib :synopsis: FTP protocol client (requires sockets).
Source code: :source:`Lib/ftplib.py`
.. index:: pair: FTP; protocol single: FTP; ftplib (standard module)
This module defines the class :class:`FTP` and a few related items. The :class:`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 :mod:`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.
Here's a sample session using the :mod:`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.'
.. exception:: error_reply Exception raised when an unexpected reply is received from the server.
.. exception:: error_temp Exception raised when an error code signifying a temporary error (response codes in the range 400--499) is received.
.. exception:: error_perm Exception raised when an error code signifying a permanent error (response codes in the range 500--599) is received.
.. exception:: error_proto Exception raised when a reply is received from the server that does not fit the response specifications of the File Transfer Protocol, i.e. begin with a digit in the range 1--5.
.. data:: all_errors The set of all exceptions (as a tuple) that methods of :class:`FTP` instances may raise as a result of problems with the FTP connection (as opposed to programming errors made by the caller). This set includes the four exceptions listed above as well as :exc:`OSError` and :exc:`EOFError`.
.. seealso::
Module :mod:`netrc`
Parser for the :file:`.netrc` file format. The file :file:`.netrc` is
typically used by FTP clients to load user authentication information
before prompting the user.