Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Doc / lib / libftplib.tex
blobd4851ab2cf5227faed5d2b8a8f24ee40fd00ec6f
1 \section{\module{ftplib} ---
2 FTP protocol client}
4 \declaremodule{standard}{ftplib}
5 \modulesynopsis{FTP protocol client (requires sockets).}
7 \indexii{FTP}{protocol}
8 \index{FTP!\module{ftplib} (standard module)}
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}.
18 Here's a sample session using the \module{ftplib} module:
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
32 >>> ftp.retrbinary('RETR README', open('README', 'wb').write)
33 '226 Transfer complete.'
34 >>> ftp.quit()
35 \end{verbatim}
37 The module defines the following items:
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}
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}
56 \begin{excdesc}{error_reply}
57 Exception raised when an unexpected reply is received from the server.
58 \end{excdesc}
60 \begin{excdesc}{error_temp}
61 Exception raised when an error code in the range 400--499 is received.
62 \end{excdesc}
64 \begin{excdesc}{error_perm}
65 Exception raised when an error code in the range 500--599 is received.
66 \end{excdesc}
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}
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}
86 \subsection{FTP Objects \label{ftp-objects}}
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.
93 \class{FTP} instances have the following methods:
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}
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}
113 \begin{methoddesc}{getwelcome}{}
114 Return the welcome message sent by the server in reply to the initial
115 connection. (This message sometimes contains disclaimers or help
116 information that may be relevant to the user.)
117 \end{methoddesc}
119 \begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
120 Log in as the given \var{user}. The \var{passwd} and \var{acct}
121 parameters are optional and default to the empty string. If no
122 \var{user} is specified, it defaults to \code{'anonymous'}. If
123 \var{user} is \code{'anonymous'}, the default \var{passwd} is
124 \code{'anonymous@'}. This function should be called only
125 once for each instance, after a connection has been established; it
126 should not be called at all if a host and user were given when the
127 instance was created. Most FTP commands are only allowed after the
128 client has logged in.
129 \end{methoddesc}
131 \begin{methoddesc}{abort}{}
132 Abort a file transfer that is in progress. Using this does not always
133 work, but it's worth a try.
134 \end{methoddesc}
136 \begin{methoddesc}{sendcmd}{command}
137 Send a simple command string to the server and return the response
138 string.
139 \end{methoddesc}
141 \begin{methoddesc}{voidcmd}{command}
142 Send a simple command string to the server and handle the response.
143 Return nothing if a response code in the range 200--299 is received.
144 Raise an exception otherwise.
145 \end{methoddesc}
147 \begin{methoddesc}{retrbinary}{command,
148 callback\optional{, maxblocksize\optional{, rest}}}
149 Retrieve a file in binary transfer mode. \var{command} should be an
150 appropriate \samp{RETR} command: \code{'RETR \var{filename}'}.
151 The \var{callback} function is called for each block of data received,
152 with a single string argument giving the data block.
153 The optional \var{maxblocksize} argument specifies the maximum chunk size to
154 read on the low-level socket object created to do the actual transfer
155 (which will also be the largest size of the data blocks passed to
156 \var{callback}). A reasonable default is chosen. \var{rest} means the
157 same thing as in the \method{transfercmd()} method.
158 \end{methoddesc}
160 \begin{methoddesc}{retrlines}{command\optional{, callback}}
161 Retrieve a file or directory listing in \ASCII{} transfer mode.
162 \var{command} should be an appropriate \samp{RETR} command (see
163 \method{retrbinary()} or a \samp{LIST} command (usually just the string
164 \code{'LIST'}). The \var{callback} function is called for each line,
165 with the trailing CRLF stripped. The default \var{callback} prints
166 the line to \code{sys.stdout}.
167 \end{methoddesc}
169 \begin{methoddesc}{set_pasv}{boolean}
170 Enable ``passive'' mode if \var{boolean} is true, other disable
171 passive mode. (In Python 2.0 and before, passive mode was off by
172 default; in Python 2.1 and later, it is on by default.)
173 \end{methoddesc}
175 \begin{methoddesc}{storbinary}{command, file\optional{, blocksize}}
176 Store a file in binary transfer mode. \var{command} should be an
177 appropriate \samp{STOR} command: \code{"STOR \var{filename}"}.
178 \var{file} is an open file object which is read until \EOF{} using its
179 \method{read()} method in blocks of size \var{blocksize} to provide the
180 data to be stored. The \var{blocksize} argument defaults to 8192.
181 \versionchanged[default for \var{blocksize} added]{2.1}
182 \end{methoddesc}
184 \begin{methoddesc}{storlines}{command, file}
185 Store a file in \ASCII{} transfer mode. \var{command} should be an
186 appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
187 read until \EOF{} from the open file object \var{file} using its
188 \method{readline()} method to provide the data to be stored.
189 \end{methoddesc}
191 \begin{methoddesc}{transfercmd}{cmd\optional{, rest}}
192 Initiate a transfer over the data connection. If the transfer is
193 active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified
194 by \var{cmd}, and accept the connection. If the server is passive,
195 send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
196 command. Either way, return the socket for the connection.
198 If optional \var{rest} is given, a \samp{REST} command is
199 sent to the server, passing \var{rest} as an argument. \var{rest} is
200 usually a byte offset into the requested file, telling the server to
201 restart sending the file's bytes at the requested offset, skipping
202 over the initial bytes. Note however that RFC
203 959 requires only that \var{rest} be a string containing characters
204 in the printable range from ASCII code 33 to ASCII code 126. The
205 \method{transfercmd()} method, therefore, converts
206 \var{rest} to a string, but no check is
207 performed on the string's contents. If the server does
208 not recognize the \samp{REST} command, an
209 \exception{error_reply} exception will be raised. If this happens,
210 simply call \method{transfercmd()} without a \var{rest} argument.
211 \end{methoddesc}
213 \begin{methoddesc}{ntransfercmd}{cmd\optional{, rest}}
214 Like \method{transfercmd()}, but returns a tuple of the data
215 connection and the expected size of the data. If the expected size
216 could not be computed, \code{None} will be returned as the expected
217 size. \var{cmd} and \var{rest} means the same thing as in
218 \method{transfercmd()}.
219 \end{methoddesc}
221 \begin{methoddesc}{nlst}{argument\optional{, \ldots}}
222 Return a list of files as returned by the \samp{NLST} command. The
223 optional \var{argument} is a directory to list (default is the current
224 server directory). Multiple arguments can be used to pass
225 non-standard options to the \samp{NLST} command.
226 \end{methoddesc}
228 \begin{methoddesc}{dir}{argument\optional{, \ldots}}
229 Produce a directory listing as returned by the \samp{LIST} command,
230 printing it to standard output. The optional \var{argument} is a
231 directory to list (default is the current server directory). Multiple
232 arguments can be used to pass non-standard options to the \samp{LIST}
233 command. If the last argument is a function, it is used as a
234 \var{callback} function as for \method{retrlines()}; the default
235 prints to \code{sys.stdout}. This method returns \code{None}.
236 \end{methoddesc}
238 \begin{methoddesc}{rename}{fromname, toname}
239 Rename file \var{fromname} on the server to \var{toname}.
240 \end{methoddesc}
242 \begin{methoddesc}{delete}{filename}
243 Remove the file named \var{filename} from the server. If successful,
244 returns the text of the response, otherwise raises
245 \exception{error_perm} on permission errors or
246 \exception{error_reply} on other errors.
247 \end{methoddesc}
249 \begin{methoddesc}{cwd}{pathname}
250 Set the current directory on the server.
251 \end{methoddesc}
253 \begin{methoddesc}{mkd}{pathname}
254 Create a new directory on the server.
255 \end{methoddesc}
257 \begin{methoddesc}{pwd}{}
258 Return the pathname of the current directory on the server.
259 \end{methoddesc}
261 \begin{methoddesc}{rmd}{dirname}
262 Remove the directory named \var{dirname} on the server.
263 \end{methoddesc}
265 \begin{methoddesc}{size}{filename}
266 Request the size of the file named \var{filename} on the server. On
267 success, the size of the file is returned as an integer, otherwise
268 \code{None} is returned. Note that the \samp{SIZE} command is not
269 standardized, but is supported by many common server implementations.
270 \end{methoddesc}
272 \begin{methoddesc}{quit}{}
273 Send a \samp{QUIT} command to the server and close the connection.
274 This is the ``polite'' way to close a connection, but it may raise an
275 exception of the server reponds with an error to the
276 \samp{QUIT} command. This implies a call to the \method{close()}
277 method which renders the \class{FTP} instance useless for subsequent
278 calls (see below).
279 \end{methoddesc}
281 \begin{methoddesc}{close}{}
282 Close the connection unilaterally. This should not be applied to an
283 already closed connection (such as after a successful call to
284 \method{quit()}. After this call the \class{FTP} instance should not
285 be used any more (after a call to \method{close()} or
286 \method{quit()} you cannot reopen the connection by issuing another
287 \method{login()} method).
288 \end{methoddesc}