Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{ftplib}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-ftplib} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 3 | \stmodindex{ftplib} |
Fred Drake | 15bac5d | 1998-01-07 13:13:42 +0000 | [diff] [blame] | 4 | \indexii{FTP}{protocol} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 6 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 7 | This module defines the class \class{FTP} and a few related items. The |
| 8 | \class{FTP} class implements the client side of the FTP protocol. You |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 9 | can use this to write Python programs that perform a variety of |
| 10 | automated FTP jobs, such as mirroring other ftp servers. It is also |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 11 | used by the module \module{urllib} to handle URLs that use FTP. For |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 12 | more information on FTP (File Transfer Protocol), see Internet \rfc{959}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 13 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 14 | Here's a sample session using the \module{ftplib} module: |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 15 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 16 | \begin{verbatim} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 17 | >>> from ftplib import FTP |
| 18 | >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 19 | >>> ftp.login() # user anonymous, passwd user@hostname |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 20 | >>> ftp.retrlines('LIST') # list directory contents |
| 21 | total 24418 |
| 22 | drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 . |
| 23 | dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 .. |
| 24 | -rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX |
| 25 | . |
| 26 | . |
| 27 | . |
| 28 | >>> ftp.quit() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 29 | \end{verbatim} |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 31 | The module defines the following items: |
| 32 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 33 | \begin{classdesc}{FTP}{\optional{host\optional{, user\optional{, passwd\optional{, acct}}}}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 34 | Return a new instance of the \code{FTP} class. When |
| 35 | \var{host} is given, the method call \code{connect(\var{host})} is |
| 36 | made. When \var{user} is given, additionally the method call |
| 37 | \code{login(\var{user}, \var{passwd}, \var{acct})} is made (where |
| 38 | \var{passwd} and \var{acct} default to the empty string when not given). |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 39 | \end{classdesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 40 | |
| 41 | \begin{datadesc}{all_errors} |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 42 | The set of all exceptions (as a tuple) that methods of \class{FTP} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 43 | instances may raise as a result of problems with the FTP connection |
| 44 | (as opposed to programming errors made by the caller). This set |
| 45 | includes the four exceptions listed below as well as |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 46 | \exception{socket.error} and \exception{IOError}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 47 | \end{datadesc} |
| 48 | |
| 49 | \begin{excdesc}{error_reply} |
| 50 | Exception raised when an unexpected reply is received from the server. |
| 51 | \end{excdesc} |
| 52 | |
| 53 | \begin{excdesc}{error_temp} |
| 54 | Exception raised when an error code in the range 400--499 is received. |
| 55 | \end{excdesc} |
| 56 | |
| 57 | \begin{excdesc}{error_perm} |
| 58 | Exception raised when an error code in the range 500--599 is received. |
| 59 | \end{excdesc} |
| 60 | |
| 61 | \begin{excdesc}{error_proto} |
| 62 | Exception raised when a reply is received from the server that does |
| 63 | not begin with a digit in the range 1--5. |
| 64 | \end{excdesc} |
| 65 | |
| 66 | \subsection{FTP Objects} |
| 67 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 68 | \class{FTP} instances have the following methods: |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 69 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 70 | \setindexsubitem{(FTP method)} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 71 | |
| 72 | \begin{funcdesc}{set_debuglevel}{level} |
| 73 | Set the instance's debugging level. This controls the amount of |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 74 | debugging output printed. The default, \code{0}, produces no |
| 75 | debugging output. A value of \code{1} produces a moderate amount of |
| 76 | debugging output, generally a single line per request. A value of |
| 77 | \code{2} or higher produces the maximum amount of debugging output, |
| 78 | logging each line sent and received on the control connection. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 79 | \end{funcdesc} |
| 80 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 81 | \begin{funcdesc}{connect}{host\optional{, port}} |
| 82 | Connect to the given host and port. The default port number is \code{21}, as |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 83 | specified by the FTP protocol specification. It is rarely needed to |
| 84 | specify a different port number. This function should be called only |
| 85 | once for each instance; it should not be called at all if a host was |
| 86 | given when the instance was created. All other methods can only be |
| 87 | used after a connection has been made. |
| 88 | \end{funcdesc} |
| 89 | |
| 90 | \begin{funcdesc}{getwelcome}{} |
| 91 | Return the welcome message sent by the server in reply to the initial |
| 92 | connection. (This message sometimes contains disclaimers or help |
| 93 | information that may be relevant to the user.) |
| 94 | \end{funcdesc} |
| 95 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 96 | \begin{funcdesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 97 | Log in as the given \var{user}. The \var{passwd} and \var{acct} |
| 98 | parameters are optional and default to the empty string. If no |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 99 | \var{user} is specified, it defaults to \code{'anonymous'}. If |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 100 | \var{user} is \code{anonymous}, the default \var{passwd} is |
| 101 | \samp{\var{realuser}@\var{host}} where \var{realuser} is the real user |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 102 | name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 103 | variable) and \var{host} is the hostname as returned by |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 104 | \function{socket.gethostname()}. This function should be called only |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 105 | once for each instance, after a connection has been established; it |
| 106 | should not be called at all if a host and user were given when the |
| 107 | instance was created. Most FTP commands are only allowed after the |
| 108 | client has logged in. |
| 109 | \end{funcdesc} |
| 110 | |
| 111 | \begin{funcdesc}{abort}{} |
| 112 | Abort a file transfer that is in progress. Using this does not always |
| 113 | work, but it's worth a try. |
| 114 | \end{funcdesc} |
| 115 | |
| 116 | \begin{funcdesc}{sendcmd}{command} |
| 117 | Send a simple command string to the server and return the response |
| 118 | string. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{voidcmd}{command} |
| 122 | Send a simple command string to the server and handle the response. |
| 123 | Return nothing if a response code in the range 200--299 is received. |
| 124 | Raise an exception otherwise. |
| 125 | \end{funcdesc} |
| 126 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 127 | \begin{funcdesc}{retrbinary}{command, callback\optional{, maxblocksize}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 128 | Retrieve a file in binary transfer mode. \var{command} should be an |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 129 | appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 130 | The \var{callback} function is called for each block of data received, |
| 131 | with a single string argument giving the data block. |
Guido van Rossum | ab76af3 | 1997-12-03 19:34:14 +0000 | [diff] [blame] | 132 | The optional \var{maxblocksize} argument specifies the maximum chunk size to |
| 133 | read on the low-level socket object created to do the actual transfer |
| 134 | (which will also be the largest size of the data blocks passed to |
| 135 | \var{callback}). A reasonable default is chosen. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 136 | \end{funcdesc} |
| 137 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 138 | \begin{funcdesc}{retrlines}{command\optional{, callback}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 139 | Retrieve a file or directory listing in \ASCII{} transfer mode. |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 140 | \var{command} should be an appropriate \samp{RETR} command (see |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 141 | \method{retrbinary()} or a \samp{LIST} command (usually just the string |
| 142 | \code{'LIST'}). The \var{callback} function is called for each line, |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 143 | with the trailing CRLF stripped. The default \var{callback} prints |
| 144 | the line to \code{sys.stdout}. |
| 145 | \end{funcdesc} |
| 146 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 147 | \begin{funcdesc}{storbinary}{command, file, blocksize} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 148 | Store a file in binary transfer mode. \var{command} should be an |
| 149 | appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}. |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 150 | \var{file} is an open file object which is read until \EOF{} using its |
| 151 | \method{read()} method in blocks of size \var{blocksize} to provide the |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 152 | data to be stored. |
| 153 | \end{funcdesc} |
| 154 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 155 | \begin{funcdesc}{storlines}{command, file} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 156 | Store a file in \ASCII{} transfer mode. \var{command} should be an |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 157 | appropriate \samp{STOR} command (see \method{storbinary()}). Lines are |
| 158 | read until \EOF{} from the open file object \var{file} using its |
| 159 | \method{readline()} method to privide the data to be stored. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 160 | \end{funcdesc} |
| 161 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 162 | \begin{funcdesc}{nlst}{argument\optional{, \ldots}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 163 | Return a list of files as returned by the \samp{NLST} command. The |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 164 | optional \var{argument} is a directory to list (default is the current |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 165 | server directory). Multiple arguments can be used to pass |
| 166 | non-standard options to the \samp{NLST} command. |
| 167 | \end{funcdesc} |
| 168 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 169 | \begin{funcdesc}{dir}{argument\optional{, \ldots}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 170 | Return a directory listing as returned by the \samp{LIST} command, as |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 171 | a list of lines. The optional \var{argument} is a directory to list |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 172 | (default is the current server directory). Multiple arguments can be |
| 173 | used to pass non-standard options to the \samp{LIST} command. If the |
| 174 | last argument is a function, it is used as a \var{callback} function |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 175 | as for \method{retrlines()}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 176 | \end{funcdesc} |
| 177 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 178 | \begin{funcdesc}{rename}{fromname, toname} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 179 | Rename file \var{fromname} on the server to \var{toname}. |
| 180 | \end{funcdesc} |
| 181 | |
| 182 | \begin{funcdesc}{cwd}{pathname} |
| 183 | Set the current directory on the server. |
| 184 | \end{funcdesc} |
| 185 | |
| 186 | \begin{funcdesc}{mkd}{pathname} |
| 187 | Create a new directory on the server. |
| 188 | \end{funcdesc} |
| 189 | |
| 190 | \begin{funcdesc}{pwd}{} |
| 191 | Return the pathname of the current directory on the server. |
| 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{quit}{} |
| 195 | Send a \samp{QUIT} command to the server and close the connection. |
| 196 | This is the ``polite'' way to close a connection, but it may raise an |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 197 | exception of the server reponds with an error to the \samp{QUIT} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 198 | command. |
| 199 | \end{funcdesc} |
| 200 | |
| 201 | \begin{funcdesc}{close}{} |
| 202 | Close the connection unilaterally. This should not be applied to an |
| 203 | already closed connection (e.g.\ after a successful call to |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 204 | \method{quit()}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 205 | \end{funcdesc} |