source: vendor/python/2.5/Doc/lib/libftplib.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 12.0 KB
Line 
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
10This module defines the class \class{FTP} and a few related items.
11The \class{FTP} class implements the client side of the FTP
12protocol. You can use this to write Python
13programs that perform a variety of automated FTP jobs, such as
14mirroring other ftp servers. It is also used by the module
15\refmodule{urllib} to handle URLs that use FTP. For more information
16on FTP (File Transfer Protocol), see Internet \rfc{959}.
17
18Here'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
25total 24418
26drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
27dr-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
37The module defines the following items:
38
39\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
40 passwd\optional{, acct}}}}}
41Return a new instance of the \class{FTP} class. When
42\var{host} is given, the method call \code{connect(\var{host})} is
43made. 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}
49The set of all exceptions (as a tuple) that methods of \class{FTP}
50instances may raise as a result of problems with the FTP connection
51(as opposed to programming errors made by the caller). This set
52includes the four exceptions listed below as well as
53\exception{socket.error} and \exception{IOError}.
54\end{datadesc}
55
56\begin{excdesc}{error_reply}
57Exception raised when an unexpected reply is received from the server.
58\end{excdesc}
59
60\begin{excdesc}{error_temp}
61Exception raised when an error code in the range 400--499 is received.
62\end{excdesc}
63
64\begin{excdesc}{error_perm}
65Exception raised when an error code in the range 500--599 is received.
66\end{excdesc}
67
68\begin{excdesc}{error_proto}
69Exception raised when a reply is received from the server that does
70not 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
88Several methods are available in two flavors: one for handling text
89files and another for binary files. These are named for the command
90which 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}
96Set the instance's debugging level. This controls the amount of
97debugging output printed. The default, \code{0}, produces no
98debugging output. A value of \code{1} produces a moderate amount of
99debugging output, generally a single line per request. A value of
100\code{2} or higher produces the maximum amount of debugging output,
101logging each line sent and received on the control connection.
102\end{methoddesc}
103
104\begin{methoddesc}{connect}{host\optional{, port}}
105Connect to the given host and port. The default port number is \code{21}, as
106specified by the FTP protocol specification. It is rarely needed to
107specify a different port number. This function should be called only
108once for each instance; it should not be called at all if a host was
109given when the instance was created. All other methods can only be
110used after a connection has been made.
111\end{methoddesc}
112
113\begin{methoddesc}{getwelcome}{}
114Return the welcome message sent by the server in reply to the initial
115connection. (This message sometimes contains disclaimers or help
116information that may be relevant to the user.)
117\end{methoddesc}
118
119\begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
120Log in as the given \var{user}. The \var{passwd} and \var{acct}
121parameters 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
125once for each instance, after a connection has been established; it
126should not be called at all if a host and user were given when the
127instance was created. Most FTP commands are only allowed after the
128client has logged in.
129\end{methoddesc}
130
131\begin{methoddesc}{abort}{}
132Abort a file transfer that is in progress. Using this does not always
133work, but it's worth a try.
134\end{methoddesc}
135
136\begin{methoddesc}{sendcmd}{command}
137Send a simple command string to the server and return the response
138string.
139\end{methoddesc}
140
141\begin{methoddesc}{voidcmd}{command}
142Send a simple command string to the server and handle the response.
143Return nothing if a response code in the range 200--299 is received.
144Raise an exception otherwise.
145\end{methoddesc}
146
147\begin{methoddesc}{retrbinary}{command,
148 callback\optional{, maxblocksize\optional{, rest}}}
149Retrieve a file in binary transfer mode. \var{command} should be an
150appropriate \samp{RETR} command: \code{'RETR \var{filename}'}.
151The \var{callback} function is called for each block of data received,
152with a single string argument giving the data block.
153The optional \var{maxblocksize} argument specifies the maximum chunk size to
154read 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
157same thing as in the \method{transfercmd()} method.
158\end{methoddesc}
159
160\begin{methoddesc}{retrlines}{command\optional{, callback}}
161Retrieve 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,
165with the trailing CRLF stripped. The default \var{callback} prints
166the line to \code{sys.stdout}.
167\end{methoddesc}
168
169\begin{methoddesc}{set_pasv}{boolean}
170Enable ``passive'' mode if \var{boolean} is true, other disable
171passive mode. (In Python 2.0 and before, passive mode was off by
172default; in Python 2.1 and later, it is on by default.)
173\end{methoddesc}
174
175\begin{methoddesc}{storbinary}{command, file\optional{, blocksize}}
176Store a file in binary transfer mode. \var{command} should be an
177appropriate \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
180data to be stored. The \var{blocksize} argument defaults to 8192.
181\versionchanged[default for \var{blocksize} added]{2.1}
182\end{methoddesc}
183
184\begin{methoddesc}{storlines}{command, file}
185Store a file in \ASCII{} transfer mode. \var{command} should be an
186appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
187read 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}
190
191\begin{methoddesc}{transfercmd}{cmd\optional{, rest}}
192Initiate a transfer over the data connection. If the transfer is
193active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified
194by \var{cmd}, and accept the connection. If the server is passive,
195send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
196command. Either way, return the socket for the connection.
197
198If optional \var{rest} is given, a \samp{REST} command is
199sent to the server, passing \var{rest} as an argument. \var{rest} is
200usually a byte offset into the requested file, telling the server to
201restart sending the file's bytes at the requested offset, skipping
202over the initial bytes. Note however that RFC
203959 requires only that \var{rest} be a string containing characters
204in 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
207performed on the string's contents. If the server does
208not recognize the \samp{REST} command, an
209\exception{error_reply} exception will be raised. If this happens,
210simply call \method{transfercmd()} without a \var{rest} argument.
211\end{methoddesc}
212
213\begin{methoddesc}{ntransfercmd}{cmd\optional{, rest}}
214Like \method{transfercmd()}, but returns a tuple of the data
215connection and the expected size of the data. If the expected size
216could not be computed, \code{None} will be returned as the expected
217size. \var{cmd} and \var{rest} means the same thing as in
218\method{transfercmd()}.
219\end{methoddesc}
220
221\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
222Return a list of files as returned by the \samp{NLST} command. The
223optional \var{argument} is a directory to list (default is the current
224server directory). Multiple arguments can be used to pass
225non-standard options to the \samp{NLST} command.
226\end{methoddesc}
227
228\begin{methoddesc}{dir}{argument\optional{, \ldots}}
229Produce a directory listing as returned by the \samp{LIST} command,
230printing it to standard output. The optional \var{argument} is a
231directory to list (default is the current server directory). Multiple
232arguments can be used to pass non-standard options to the \samp{LIST}
233command. If the last argument is a function, it is used as a
234\var{callback} function as for \method{retrlines()}; the default
235prints to \code{sys.stdout}. This method returns \code{None}.
236\end{methoddesc}
237
238\begin{methoddesc}{rename}{fromname, toname}
239Rename file \var{fromname} on the server to \var{toname}.
240\end{methoddesc}
241
242\begin{methoddesc}{delete}{filename}
243Remove the file named \var{filename} from the server. If successful,
244returns 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}
248
249\begin{methoddesc}{cwd}{pathname}
250Set the current directory on the server.
251\end{methoddesc}
252
253\begin{methoddesc}{mkd}{pathname}
254Create a new directory on the server.
255\end{methoddesc}
256
257\begin{methoddesc}{pwd}{}
258Return the pathname of the current directory on the server.
259\end{methoddesc}
260
261\begin{methoddesc}{rmd}{dirname}
262Remove the directory named \var{dirname} on the server.
263\end{methoddesc}
264
265\begin{methoddesc}{size}{filename}
266Request the size of the file named \var{filename} on the server. On
267success, the size of the file is returned as an integer, otherwise
268\code{None} is returned. Note that the \samp{SIZE} command is not
269standardized, but is supported by many common server implementations.
270\end{methoddesc}
271
272\begin{methoddesc}{quit}{}
273Send a \samp{QUIT} command to the server and close the connection.
274This is the ``polite'' way to close a connection, but it may raise an
275exception of the server reponds with an error to the
276\samp{QUIT} command. This implies a call to the \method{close()}
277method which renders the \class{FTP} instance useless for subsequent
278calls (see below).
279\end{methoddesc}
280
281\begin{methoddesc}{close}{}
282Close the connection unilaterally. This should not be applied to an
283already closed connection such as after a successful call to
284\method{quit()}. After this call the \class{FTP} instance should not
285be 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}
Note: See TracBrowser for help on using the repository browser.