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