| 1 | \section{\module{ftplib} ---
|
|---|
| 2 | FTP protocol client}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{ftplib}
|
|---|
| 5 | \modulesynopsis{FTP protocol client (requires sockets).}
|
|---|
| 6 |
|
|---|
| 7 | \indexii{FTP}{protocol}
|
|---|
| 8 | \index{FTP!\module{ftplib} (standard module)}
|
|---|
| 9 |
|
|---|
| 10 | This module defines the class \class{FTP} and a few related items.
|
|---|
| 11 | The \class{FTP} class implements the client side of the FTP
|
|---|
| 12 | protocol. You can use this to write Python
|
|---|
| 13 | programs that perform a variety of automated FTP jobs, such as
|
|---|
| 14 | mirroring other ftp servers. It is also used by the module
|
|---|
| 15 | \refmodule{urllib} to handle URLs that use FTP. For more information
|
|---|
| 16 | on FTP (File Transfer Protocol), see Internet \rfc{959}.
|
|---|
| 17 |
|
|---|
| 18 | Here's a sample session using the \module{ftplib} module:
|
|---|
| 19 |
|
|---|
| 20 | \begin{verbatim}
|
|---|
| 21 | >>> from ftplib import FTP
|
|---|
| 22 | >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
|
|---|
| 23 | >>> ftp.login() # user anonymous, passwd anonymous@
|
|---|
| 24 | >>> ftp.retrlines('LIST') # list directory contents
|
|---|
| 25 | total 24418
|
|---|
| 26 | drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
|
|---|
| 27 | dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
|
|---|
| 28 | -rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
|
|---|
| 29 | .
|
|---|
| 30 | .
|
|---|
| 31 | .
|
|---|
| 32 | >>> ftp.retrbinary('RETR README', open('README', 'wb').write)
|
|---|
| 33 | '226 Transfer complete.'
|
|---|
| 34 | >>> ftp.quit()
|
|---|
| 35 | \end{verbatim}
|
|---|
| 36 |
|
|---|
| 37 | The module defines the following items:
|
|---|
| 38 |
|
|---|
| 39 | \begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
|
|---|
| 40 | passwd\optional{, acct}}}}}
|
|---|
| 41 | Return a new instance of the \class{FTP} class. When
|
|---|
| 42 | \var{host} is given, the method call \code{connect(\var{host})} is
|
|---|
| 43 | made. When \var{user} is given, additionally the method call
|
|---|
| 44 | \code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
|
|---|
| 45 | \var{passwd} and \var{acct} default to the empty string when not given).
|
|---|
| 46 | \end{classdesc}
|
|---|
| 47 |
|
|---|
| 48 | \begin{datadesc}{all_errors}
|
|---|
| 49 | The set of all exceptions (as a tuple) that methods of \class{FTP}
|
|---|
| 50 | instances may raise as a result of problems with the FTP connection
|
|---|
| 51 | (as opposed to programming errors made by the caller). This set
|
|---|
| 52 | includes the four exceptions listed below as well as
|
|---|
| 53 | \exception{socket.error} and \exception{IOError}.
|
|---|
| 54 | \end{datadesc}
|
|---|
| 55 |
|
|---|
| 56 | \begin{excdesc}{error_reply}
|
|---|
| 57 | Exception raised when an unexpected reply is received from the server.
|
|---|
| 58 | \end{excdesc}
|
|---|
| 59 |
|
|---|
| 60 | \begin{excdesc}{error_temp}
|
|---|
| 61 | Exception raised when an error code in the range 400--499 is received.
|
|---|
| 62 | \end{excdesc}
|
|---|
| 63 |
|
|---|
| 64 | \begin{excdesc}{error_perm}
|
|---|
| 65 | Exception raised when an error code in the range 500--599 is received.
|
|---|
| 66 | \end{excdesc}
|
|---|
| 67 |
|
|---|
| 68 | \begin{excdesc}{error_proto}
|
|---|
| 69 | Exception raised when a reply is received from the server that does
|
|---|
| 70 | not begin with a digit in the range 1--5.
|
|---|
| 71 | \end{excdesc}
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | \begin{seealso}
|
|---|
| 75 | \seemodule{netrc}{Parser for the \file{.netrc} file format. The file
|
|---|
| 76 | \file{.netrc} is typically used by FTP clients to
|
|---|
| 77 | load user authentication information before prompting
|
|---|
| 78 | the user.}
|
|---|
| 79 | \seetext{The file \file{Tools/scripts/ftpmirror.py}\index{ftpmirror.py}
|
|---|
| 80 | in the Python source distribution is a script that can mirror
|
|---|
| 81 | FTP sites, or portions thereof, using the \module{ftplib} module.
|
|---|
| 82 | It can be used as an extended example that applies this module.}
|
|---|
| 83 | \end{seealso}
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | \subsection{FTP Objects \label{ftp-objects}}
|
|---|
| 87 |
|
|---|
| 88 | Several methods are available in two flavors: one for handling text
|
|---|
| 89 | files and another for binary files. These are named for the command
|
|---|
| 90 | which is used followed by \samp{lines} for the text version or
|
|---|
| 91 | \samp{binary} for the binary version.
|
|---|
| 92 |
|
|---|
| 93 | \class{FTP} instances have the following methods:
|
|---|
| 94 |
|
|---|
| 95 | \begin{methoddesc}{set_debuglevel}{level}
|
|---|
| 96 | Set the instance's debugging level. This controls the amount of
|
|---|
| 97 | debugging output printed. The default, \code{0}, produces no
|
|---|
| 98 | debugging output. A value of \code{1} produces a moderate amount of
|
|---|
| 99 | debugging output, generally a single line per request. A value of
|
|---|
| 100 | \code{2} or higher produces the maximum amount of debugging output,
|
|---|
| 101 | logging each line sent and received on the control connection.
|
|---|
| 102 | \end{methoddesc}
|
|---|
| 103 |
|
|---|
| 104 | \begin{methoddesc}{connect}{host\optional{, port}}
|
|---|
| 105 | Connect to the given host and port. The default port number is \code{21}, as
|
|---|
| 106 | specified by the FTP protocol specification. It is rarely needed to
|
|---|
| 107 | specify a different port number. This function should be called only
|
|---|
| 108 | once for each instance; it should not be called at all if a host was
|
|---|
| 109 | given when the instance was created. All other methods can only be
|
|---|
| 110 | used after a connection has been made.
|
|---|
| 111 | \end{methoddesc}
|
|---|
| 112 |
|
|---|
| 113 | \begin{methoddesc}{getwelcome}{}
|
|---|
|
|---|