Merged release21-maint changes.
[python/dscho.git] / Doc / lib / libftplib.tex
blobe7e534174a8422836e1c42f9d998d35c50e75af6
1 \section{\module{ftplib} ---
2 FTP protocol client}
4 \declaremodule{standard}{ftplib}
5 \modulesynopsis{FTP protocol client (requires sockets).}
8 This module defines the class \class{FTP} and a few related items.
9 The \class{FTP} class implements the client side of the FTP
10 protocol.\indexii{FTP}{protocol} You can use this to write Python
11 programs that perform a variety of automated FTP jobs, such as
12 mirroring other ftp servers. It is also used by the module
13 \refmodule{urllib} to handle URLs that use FTP. For more information
14 on FTP (File Transfer Protocol), see Internet \rfc{959}.
16 Here's a sample session using the \module{ftplib} module:
18 \begin{verbatim}
19 >>> from ftplib import FTP
20 >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
21 >>> ftp.login() # user anonymous, passwd user@hostname
22 >>> ftp.retrlines('LIST') # list directory contents
23 total 24418
24 drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
25 dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
26 -rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
30 >>> ftp.retrbinary('RETR README', open('README', 'wb').write)
31 '226 Transfer complete.'
32 >>> ftp.quit()
33 \end{verbatim}
35 The module defines the following items:
37 \begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
38 passwd\optional{, acct}}}}}
39 Return a new instance of the \class{FTP} class. When
40 \var{host} is given, the method call \code{connect(\var{host})} is
41 made. When \var{user} is given, additionally the method call
42 \code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
43 \var{passwd} and \var{acct} default to the empty string when not given).
44 \end{classdesc}
46 \begin{datadesc}{all_errors}
47 The set of all exceptions (as a tuple) that methods of \class{FTP}
48 instances may raise as a result of problems with the FTP connection
49 (as opposed to programming errors made by the caller). This set
50 includes the four exceptions listed below as well as
51 \exception{socket.error} and \exception{IOError}.
52 \end{datadesc}
54 \begin{excdesc}{error_reply}
55 Exception raised when an unexpected reply is received from the server.
56 \end{excdesc}
58 \begin{excdesc}{error_temp}
59 Exception raised when an error code in the range 400--499 is received.
60 \end{excdesc}
62 \begin{excdesc}{error_perm}
63 Exception raised when an error code in the range 500--599 is received.
64 \end{excdesc}
66 \begin{excdesc}{error_proto}
67 Exception raised when a reply is received from the server that does
68 not begin with a digit in the range 1--5.
69 \end{excdesc}
72 \begin{seealso}
73 \seemodule{netrc}{Parser for the \file{.netrc} file format. The file
74 \file{.netrc} is typically used by FTP clients to
75 load user authentication information before prompting
76 the user.}
77 \seetext{The file \file{Tools/scripts/ftpmirror.py}\index{ftpmirror.py}
78 in the Python source distribution is a script that can mirror
79 FTP sites, or portions thereof, using the \module{ftplib} module.
80 It can be used as an extended example that applies this module.}
81 \end{seealso}
84 \subsection{FTP Objects \label{ftp-objects}}
86 Several methods are available in two flavors: one for handling text
87 files and another for binary files. These are named for the command
88 which is used followed by \samp{lines} for the text version or
89 \samp{binary} for the binary version.
91 \class{FTP} instances have the following methods:
93 \begin{methoddesc}{set_debuglevel}{level}
94 Set the instance's debugging level. This controls the amount of
95 debugging output printed. The default, \code{0}, produces no
96 debugging output. A value of \code{1} produces a moderate amount of
97 debugging output, generally a single line per request. A value of
98 \code{2} or higher produces the maximum amount of debugging output,
99 logging each line sent and received on the control connection.
100 \end{methoddesc}
102 \begin{methoddesc}{connect}{host\optional{, port}}
103 Connect to the given host and port. The default port number is \code{21}, as
104 specified by the FTP protocol specification. It is rarely needed to
105 specify a different port number. This function should be called only
106 once for each instance; it should not be called at all if a host was
107 given when the instance was created. All other methods can only be
108 used after a connection has been made.
109 \end{methoddesc}
111 \begin{methoddesc}{getwelcome}{}
112 Return the welcome message sent by the server in reply to the initial
113 connection. (This message sometimes contains disclaimers or help
114 information that may be relevant to the user.)
115 \end{methoddesc}
117 \begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
118 Log in as the given \var{user}. The \var{passwd} and \var{acct}
119 parameters are optional and default to the empty string. If no
120 \var{user} is specified, it defaults to \code{'anonymous'}. If
121 \var{user} is \code{'anonymous'}, the default \var{passwd} is
122 \samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
123 name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
124 variable) and \var{host} is the hostname as returned by
125 \function{socket.gethostname()}. This function should be called only
126 once for each instance, after a connection has been established; it
127 should not be called at all if a host and user were given when the
128 instance was created. Most FTP commands are only allowed after the
129 client has logged in.
130 \end{methoddesc}
132 \begin{methoddesc}{abort}{}
133 Abort a file transfer that is in progress. Using this does not always
134 work, but it's worth a try.
135 \end{methoddesc}
137 \begin{methoddesc}{sendcmd}{command}
138 Send a simple command string to the server and return the response
139 string.
140 \end{methoddesc}
142 \begin{methoddesc}{voidcmd}{command}
143 Send a simple command string to the server and handle the response.
144 Return nothing if a response code in the range 200--299 is received.
145 Raise an exception otherwise.
146 \end{methoddesc}
148 \begin{methoddesc}{retrbinary}{command,
149 callback\optional{, maxblocksize\optional{, rest}}}
150 Retrieve a file in binary transfer mode. \var{command} should be an
151 appropriate \samp{RETR} command: \code{'RETR \var{filename}'}.
152 The \var{callback} function is called for each block of data received,
153 with a single string argument giving the data block.
154 The optional \var{maxblocksize} argument specifies the maximum chunk size to
155 read on the low-level socket object created to do the actual transfer
156 (which will also be the largest size of the data blocks passed to
157 \var{callback}). A reasonable default is chosen. \var{rest} means the
158 same thing as in the \method{transfercmd()} method.
159 \end{methoddesc}
161 \begin{methoddesc}{retrlines}{command\optional{, callback}}
162 Retrieve a file or directory listing in \ASCII{} transfer mode.
163 \var{command} should be an appropriate \samp{RETR} command (see
164 \method{retrbinary()} or a \samp{LIST} command (usually just the string
165 \code{'LIST'}). The \var{callback} function is called for each line,
166 with the trailing CRLF stripped. The default \var{callback} prints
167 the line to \code{sys.stdout}.
168 \end{methoddesc}
170 \begin{methoddesc}{set_pasv}{boolean}
171 Enable ``passive'' mode if \var{boolean} is true, other disable
172 passive mode. (In Python 2.0 and before, passive mode was off by
173 default; in Python 2.1 and later, it is on by default.)
174 \end{methoddesc}
176 \begin{methoddesc}{storbinary}{command, file\optional{, blocksize}}
177 Store a file in binary transfer mode. \var{command} should be an
178 appropriate \samp{STOR} command: \code{"STOR \var{filename}"}.
179 \var{file} is an open file object which is read until \EOF{} using its
180 \method{read()} method in blocks of size \var{blocksize} to provide the
181 data to be stored. The \var{blocksize} argument defaults to 8192.
182 \versionchanged[default for \var{blocksize} added]{2.1}
183 \end{methoddesc}
185 \begin{methoddesc}{storlines}{command, file}
186 Store a file in \ASCII{} transfer mode. \var{command} should be an
187 appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
188 read until \EOF{} from the open file object \var{file} using its
189 \method{readline()} method to provide the data to be stored.
190 \end{methoddesc}
192 \begin{methoddesc}{transfercmd}{cmd\optional{, rest}}
193 Initiate a transfer over the data connection. If the transfer is
194 active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified
195 by \var{cmd}, and accept the connection. If the server is passive,
196 send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
197 command. Either way, return the socket for the connection.
199 If optional \var{rest} is given, a \samp{REST} command is
200 sent to the server, passing \var{rest} as an argument. \var{rest} is
201 usually a byte offset into the requested file, telling the server to
202 restart sending the file's bytes at the requested offset, skipping
203 over the initial bytes. Note however that RFC
204 959 requires only that \var{rest} be a string containing characters
205 in the printable range from ASCII code 33 to ASCII code 126. The
206 \method{transfercmd()} method, therefore, converts
207 \var{rest} to a string, but no check is
208 performed on the string's contents. If the server does
209 not recognize the \samp{REST} command, an
210 \exception{error_reply} exception will be raised. If this happens,
211 simply call \method{transfercmd()} without a \var{rest} argument.
212 \end{methoddesc}
214 \begin{methoddesc}{ntransfercmd}{cmd\optional{, rest}}
215 Like \method{transfercmd()}, but returns a tuple of the data
216 connection and the expected size of the data. If the expected size
217 could not be computed, \code{None} will be returned as the expected
218 size. \var{cmd} and \var{rest} means the same thing as in
219 \method{transfercmd()}.
220 \end{methoddesc}
222 \begin{methoddesc}{nlst}{argument\optional{, \ldots}}
223 Return a list of files as returned by the \samp{NLST} command. The
224 optional \var{argument} is a directory to list (default is the current
225 server directory). Multiple arguments can be used to pass
226 non-standard options to the \samp{NLST} command.
227 \end{methoddesc}
229 \begin{methoddesc}{dir}{argument\optional{, \ldots}}
230 Produce a directory listing as returned by the \samp{LIST} command,
231 printing it to standard output. The optional \var{argument} is a
232 directory to list (default is the current server directory). Multiple
233 arguments can be used to pass non-standard options to the \samp{LIST}
234 command. If the last argument is a function, it is used as a
235 \var{callback} function as for \method{retrlines()}; the default
236 prints to \code{sys.stdout}. This method returns \code{None}.
237 \end{methoddesc}
239 \begin{methoddesc}{rename}{fromname, toname}
240 Rename file \var{fromname} on the server to \var{toname}.
241 \end{methoddesc}
243 \begin{methoddesc}{delete}{filename}
244 Remove the file named \var{filename} from the server. If successful,
245 returns the text of the response, otherwise raises
246 \exception{error_perm} on permission errors or
247 \exception{error_reply} on other errors.
248 \end{methoddesc}
250 \begin{methoddesc}{cwd}{pathname}
251 Set the current directory on the server.
252 \end{methoddesc}
254 \begin{methoddesc}{mkd}{pathname}
255 Create a new directory on the server.
256 \end{methoddesc}
258 \begin{methoddesc}{pwd}{}
259 Return the pathname of the current directory on the server.
260 \end{methoddesc}
262 \begin{methoddesc}{rmd}{dirname}
263 Remove the directory named \var{dirname} on the server.
264 \end{methoddesc}
266 \begin{methoddesc}{size}{filename}
267 Request the size of the file named \var{filename} on the server. On
268 success, the size of the file is returned as an integer, otherwise
269 \code{None} is returned. Note that the \samp{SIZE} command is not
270 standardized, but is supported by many common server implementations.
271 \end{methoddesc}
273 \begin{methoddesc}{quit}{}
274 Send a \samp{QUIT} command to the server and close the connection.
275 This is the ``polite'' way to close a connection, but it may raise an
276 exception of the server reponds with an error to the
277 \samp{QUIT} command. This implies a call to the \method{close()}
278 method which renders the \class{FTP} instance useless for subsequent
279 calls (see below).
280 \end{methoddesc}
282 \begin{methoddesc}{close}{}
283 Close the connection unilaterally. This should not be applied to an
284 already closed connection (such as after a successful call to
285 \method{quit()}. After this call the \class{FTP} instance should not
286 be used any more (after a call to \method{close()} or
287 \method{quit()} you cannot reopen the connection by issuing another
288 \method{login()} method).
289 \end{methoddesc}