blob: 166fd6e75579dbda2382c4ef68e73a5bd5db683d [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{ftplib}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{ftplib}
3
4\modulesynopsis{FTP protocol client (requires sockets).}
5
Fred Drake15bac5d1998-01-07 13:13:42 +00006\indexii{FTP}{protocol}
Guido van Rossum86751151995-02-28 17:14:32 +00007
Guido van Rossum86751151995-02-28 17:14:32 +00008
Fred Drake4f316941998-04-27 14:54:06 +00009This module defines the class \class{FTP} and a few related items.
10The \class{FTP} class implements the client side of the FTP protocol.
11You can use this to write Python programs that perform a variety of
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000012automated FTP jobs, such as mirroring other ftp servers. It is also
Fred Drake6a1eefe1998-03-12 06:04:53 +000013used by the module \module{urllib} to handle URLs that use FTP. For
Fred Drake4f316941998-04-27 14:54:06 +000014more information on FTP (File Transfer Protocol), see Internet
15\rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000016
Fred Drake6a1eefe1998-03-12 06:04:53 +000017Here's a sample session using the \module{ftplib} module:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000018
Fred Drake19479911998-02-13 06:58:54 +000019\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000020>>> from ftplib import FTP
21>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000022>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000023>>> ftp.retrlines('LIST') # list directory contents
24total 24418
25drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
26dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
27-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
28 .
29 .
30 .
Fred Drake161edc21998-08-07 17:30:49 +000031>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
32'226 Transfer complete.'
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000033>>> ftp.quit()
Fred Drake19479911998-02-13 06:58:54 +000034\end{verbatim}
Fred Drake6a1eefe1998-03-12 06:04:53 +000035
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000036The module defines the following items:
37
Fred Drakefc576191998-04-04 07:15:02 +000038\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
39 passwd\optional{, acct}}}}}
Fred Drake4f316941998-04-27 14:54:06 +000040Return a new instance of the \class{FTP} class. When
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000041\var{host} is given, the method call \code{connect(\var{host})} is
42made. When \var{user} is given, additionally the method call
43\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
44\var{passwd} and \var{acct} default to the empty string when not given).
Fred Drake6a1eefe1998-03-12 06:04:53 +000045\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000046
47\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000048The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000049instances may raise as a result of problems with the FTP connection
50(as opposed to programming errors made by the caller). This set
51includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000052\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000053\end{datadesc}
54
55\begin{excdesc}{error_reply}
56Exception raised when an unexpected reply is received from the server.
57\end{excdesc}
58
59\begin{excdesc}{error_temp}
60Exception raised when an error code in the range 400--499 is received.
61\end{excdesc}
62
63\begin{excdesc}{error_perm}
64Exception raised when an error code in the range 500--599 is received.
65\end{excdesc}
66
67\begin{excdesc}{error_proto}
68Exception raised when a reply is received from the server that does
69not begin with a digit in the range 1--5.
70\end{excdesc}
71
Fred Drakefc576191998-04-04 07:15:02 +000072
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000073\subsection{FTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000074\label{ftp-objects}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000075
Fred Drake6a1eefe1998-03-12 06:04:53 +000076\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000077
Fred Drakefc576191998-04-04 07:15:02 +000078\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000079Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000080debugging output printed. The default, \code{0}, produces no
81debugging output. A value of \code{1} produces a moderate amount of
82debugging output, generally a single line per request. A value of
83\code{2} or higher produces the maximum amount of debugging output,
84logging each line sent and received on the control connection.
Fred Drakefc576191998-04-04 07:15:02 +000085\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000086
Fred Drakefc576191998-04-04 07:15:02 +000087\begin{methoddesc}{connect}{host\optional{, port}}
Fred Drake6a1eefe1998-03-12 06:04:53 +000088Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000089specified by the FTP protocol specification. It is rarely needed to
90specify a different port number. This function should be called only
91once for each instance; it should not be called at all if a host was
92given when the instance was created. All other methods can only be
93used after a connection has been made.
Fred Drakefc576191998-04-04 07:15:02 +000094\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000097Return the welcome message sent by the server in reply to the initial
98connection. (This message sometimes contains disclaimers or help
99information that may be relevant to the user.)
Fred Drakefc576191998-04-04 07:15:02 +0000100\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000101
Fred Drakefc576191998-04-04 07:15:02 +0000102\begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000103Log in as the given \var{user}. The \var{passwd} and \var{acct}
104parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000105\var{user} is specified, it defaults to \code{'anonymous'}. If
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000106\var{user} is \code{anonymous}, the default \var{passwd} is
107\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000108name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000109variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000110\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000111once for each instance, after a connection has been established; it
112should not be called at all if a host and user were given when the
113instance was created. Most FTP commands are only allowed after the
114client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000115\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000116
Fred Drakefc576191998-04-04 07:15:02 +0000117\begin{methoddesc}{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000118Abort a file transfer that is in progress. Using this does not always
119work, but it's worth a try.
Fred Drakefc576191998-04-04 07:15:02 +0000120\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000121
Fred Drakefc576191998-04-04 07:15:02 +0000122\begin{methoddesc}{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000123Send a simple command string to the server and return the response
124string.
Fred Drakefc576191998-04-04 07:15:02 +0000125\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000126
Fred Drakefc576191998-04-04 07:15:02 +0000127\begin{methoddesc}{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000128Send a simple command string to the server and handle the response.
129Return nothing if a response code in the range 200--299 is received.
130Raise an exception otherwise.
Fred Drakefc576191998-04-04 07:15:02 +0000131\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000132
Fred Drakefc576191998-04-04 07:15:02 +0000133\begin{methoddesc}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000134Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000135appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000136The \var{callback} function is called for each block of data received,
137with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000138The optional \var{maxblocksize} argument specifies the maximum chunk size to
139read on the low-level socket object created to do the actual transfer
140(which will also be the largest size of the data blocks passed to
141\var{callback}). A reasonable default is chosen.
Fred Drakefc576191998-04-04 07:15:02 +0000142\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000143
Fred Drakefc576191998-04-04 07:15:02 +0000144\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000145Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000146\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000147\method{retrbinary()} or a \samp{LIST} command (usually just the string
148\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000149with the trailing CRLF stripped. The default \var{callback} prints
150the line to \code{sys.stdout}.
Fred Drakefc576191998-04-04 07:15:02 +0000151\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000152
Fred Drakefc576191998-04-04 07:15:02 +0000153\begin{methoddesc}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000154Store a file in binary transfer mode. \var{command} should be an
155appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000156\var{file} is an open file object which is read until \EOF{} using its
157\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000158data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000159\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000160
Fred Drakefc576191998-04-04 07:15:02 +0000161\begin{methoddesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000162Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000163appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
164read until \EOF{} from the open file object \var{file} using its
165\method{readline()} method to privide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000166\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000167
Fred Drake4f316941998-04-27 14:54:06 +0000168\begin{methoddesc}{transfercmd}{cmd}
169Initiate a transfer over the data connection. If the transfer is
170active, send a \samp{PORT} command and the transfer command specified
171by \var{cmd}, and accept the connection. If the server is passive,
172send a \samp{PASV} command, connect to it, and start the transfer
173command. Either way, return the socket for the connection.
174\end{methoddesc}
175
176\begin{methoddesc}{ntransfercmd}{cmd}
177Like \method{transfercmd()}, but returns a tuple of the data
178connection and the expected size of the data. If the expected size
179could not be computed, \code{None} will be returned as the expected
180size.
181\end{methoddesc}
182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000184Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000185optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000186server directory). Multiple arguments can be used to pass
187non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000188\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000189
Fred Drakefc576191998-04-04 07:15:02 +0000190\begin{methoddesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000191Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000192a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000193(default is the current server directory). Multiple arguments can be
194used to pass non-standard options to the \samp{LIST} command. If the
195last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000196as for \method{retrlines()}.
Fred Drakefc576191998-04-04 07:15:02 +0000197\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000198
Fred Drakefc576191998-04-04 07:15:02 +0000199\begin{methoddesc}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000200Rename file \var{fromname} on the server to \var{toname}.
Fred Drakefc576191998-04-04 07:15:02 +0000201\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000202
Fred Drake4f316941998-04-27 14:54:06 +0000203\begin{methoddesc}{delete}{filename}
204Remove the file named \var{filename} from the server. If successful,
205returns the text of the response, otherwise raises
206\exception{error_perm} on permission errors or \exception{error_reply}
207on other errors.
208\end{methoddesc}
209
Fred Drakefc576191998-04-04 07:15:02 +0000210\begin{methoddesc}{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000211Set the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000212\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000213
Fred Drakefc576191998-04-04 07:15:02 +0000214\begin{methoddesc}{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000215Create a new directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000216\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000217
Fred Drakefc576191998-04-04 07:15:02 +0000218\begin{methoddesc}{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000219Return the pathname of the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000220\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000221
Fred Drake4f316941998-04-27 14:54:06 +0000222\begin{methoddesc}{rmd}{dirname}
223Remove the directory named \var{dirname} on the server.
224\end{methoddesc}
225
226\begin{methoddesc}{size}{filename}
227Request the size of the file named \var{filename} on the server. On
228success, the size of the file is returned as an integer, otherwise
229\code{None} is returned. Note that the \samp{SIZE} command is not
230standardized, but is supported by many common server implementations.
231\end{methoddesc}
232
Fred Drakefc576191998-04-04 07:15:02 +0000233\begin{methoddesc}{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000234Send a \samp{QUIT} command to the server and close the connection.
235This is the ``polite'' way to close a connection, but it may raise an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000236exception of the server reponds with an error to the \samp{QUIT}
Guido van Rossum730d8371998-08-07 17:36:59 +0000237command. This implies a call to the \method{close()} method which
238renders the \class{FTP} instance useless for subsequent calls (see
239below).
Fred Drakefc576191998-04-04 07:15:02 +0000240\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000241
Fred Drakefc576191998-04-04 07:15:02 +0000242\begin{methoddesc}{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000243Close the connection unilaterally. This should not be applied to an
244already closed connection (e.g.\ after a successful call to
Guido van Rossum730d8371998-08-07 17:36:59 +0000245\method{quit()}. After this call the \class{FTP} instance should not
246be used any more (i.e., after a call to \method{close()} or
247\method{quit()} you cannot reopen the connection by issueing another
248\method{login()} method).
Fred Drakefc576191998-04-04 07:15:02 +0000249\end{methoddesc}