blob: 5840e83461f6b47f6424ac82e40bfaab4e546a02 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{ftplib} ---
2 FTP protocol client.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{ftplib}
4
5\modulesynopsis{FTP protocol client (requires sockets).}
6
Fred Drake15bac5d1998-01-07 13:13:42 +00007\indexii{FTP}{protocol}
Guido van Rossum86751151995-02-28 17:14:32 +00008
Guido van Rossum86751151995-02-28 17:14:32 +00009
Fred Drake4f316941998-04-27 14:54:06 +000010This module defines the class \class{FTP} and a few related items.
11The \class{FTP} class implements the client side of the FTP protocol.
12You can use this to write Python programs that perform a variety of
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000013automated FTP jobs, such as mirroring other ftp servers. It is also
Fred Drake6a1eefe1998-03-12 06:04:53 +000014used by the module \module{urllib} to handle URLs that use FTP. For
Fred Drake4f316941998-04-27 14:54:06 +000015more information on FTP (File Transfer Protocol), see Internet
16\rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000017
Fred Drake6a1eefe1998-03-12 06:04:53 +000018Here's a sample session using the \module{ftplib} module:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000019
Fred Drake19479911998-02-13 06:58:54 +000020\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000021>>> from ftplib import FTP
22>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000023>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000024>>> 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 .
Fred Drake161edc21998-08-07 17:30:49 +000032>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
33'226 Transfer complete.'
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000034>>> ftp.quit()
Fred Drake19479911998-02-13 06:58:54 +000035\end{verbatim}
Fred Drake6a1eefe1998-03-12 06:04:53 +000036
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000037The module defines the following items:
38
Fred Drakefc576191998-04-04 07:15:02 +000039\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
40 passwd\optional{, acct}}}}}
Fred Drake4f316941998-04-27 14:54:06 +000041Return a new instance of the \class{FTP} class. When
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000042\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).
Fred Drake6a1eefe1998-03-12 06:04:53 +000046\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000047
48\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000049The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000050instances 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
Fred Drake6a1eefe1998-03-12 06:04:53 +000053\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000054\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
Fred Drakefc576191998-04-04 07:15:02 +000073
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000074\subsection{FTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000075\label{ftp-objects}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000076
Fred Drake6a1eefe1998-03-12 06:04:53 +000077\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000078
Fred Drakefc576191998-04-04 07:15:02 +000079\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000080Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000081debugging output printed. The default, \code{0}, produces no
82debugging output. A value of \code{1} produces a moderate amount of
83debugging output, generally a single line per request. A value of
84\code{2} or higher produces the maximum amount of debugging output,
85logging each line sent and received on the control connection.
Fred Drakefc576191998-04-04 07:15:02 +000086\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000087
Fred Drakefc576191998-04-04 07:15:02 +000088\begin{methoddesc}{connect}{host\optional{, port}}
Fred Drake6a1eefe1998-03-12 06:04:53 +000089Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000090specified by the FTP protocol specification. It is rarely needed to
91specify a different port number. This function should be called only
92once for each instance; it should not be called at all if a host was
93given when the instance was created. All other methods can only be
94used after a connection has been made.
Fred Drakefc576191998-04-04 07:15:02 +000095\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000096
Fred Drakefc576191998-04-04 07:15:02 +000097\begin{methoddesc}{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000098Return the welcome message sent by the server in reply to the initial
99connection. (This message sometimes contains disclaimers or help
100information that may be relevant to the user.)
Fred Drakefc576191998-04-04 07:15:02 +0000101\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000102
Fred Drakefc576191998-04-04 07:15:02 +0000103\begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000104Log in as the given \var{user}. The \var{passwd} and \var{acct}
105parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000106\var{user} is specified, it defaults to \code{'anonymous'}. If
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000107\var{user} is \code{anonymous}, the default \var{passwd} is
108\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000109name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000110variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000111\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000112once for each instance, after a connection has been established; it
113should not be called at all if a host and user were given when the
114instance was created. Most FTP commands are only allowed after the
115client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000116\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000117
Fred Drakefc576191998-04-04 07:15:02 +0000118\begin{methoddesc}{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000119Abort a file transfer that is in progress. Using this does not always
120work, but it's worth a try.
Fred Drakefc576191998-04-04 07:15:02 +0000121\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000122
Fred Drakefc576191998-04-04 07:15:02 +0000123\begin{methoddesc}{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000124Send a simple command string to the server and return the response
125string.
Fred Drakefc576191998-04-04 07:15:02 +0000126\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000127
Fred Drakefc576191998-04-04 07:15:02 +0000128\begin{methoddesc}{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000129Send a simple command string to the server and handle the response.
130Return nothing if a response code in the range 200--299 is received.
131Raise an exception otherwise.
Fred Drakefc576191998-04-04 07:15:02 +0000132\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000133
Fred Drakefc576191998-04-04 07:15:02 +0000134\begin{methoddesc}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000135Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000136appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000137The \var{callback} function is called for each block of data received,
138with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000139The optional \var{maxblocksize} argument specifies the maximum chunk size to
140read on the low-level socket object created to do the actual transfer
141(which will also be the largest size of the data blocks passed to
142\var{callback}). A reasonable default is chosen.
Fred Drakefc576191998-04-04 07:15:02 +0000143\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000144
Fred Drakefc576191998-04-04 07:15:02 +0000145\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000146Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000147\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000148\method{retrbinary()} or a \samp{LIST} command (usually just the string
149\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000150with the trailing CRLF stripped. The default \var{callback} prints
151the line to \code{sys.stdout}.
Fred Drakefc576191998-04-04 07:15:02 +0000152\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000153
Fred Drakefc576191998-04-04 07:15:02 +0000154\begin{methoddesc}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000155Store a file in binary transfer mode. \var{command} should be an
156appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000157\var{file} is an open file object which is read until \EOF{} using its
158\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000159data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000160\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000161
Fred Drakefc576191998-04-04 07:15:02 +0000162\begin{methoddesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000163Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000164appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
165read until \EOF{} from the open file object \var{file} using its
166\method{readline()} method to privide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000167\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000168
Fred Drake4f316941998-04-27 14:54:06 +0000169\begin{methoddesc}{transfercmd}{cmd}
170Initiate a transfer over the data connection. If the transfer is
171active, send a \samp{PORT} command and the transfer command specified
172by \var{cmd}, and accept the connection. If the server is passive,
173send a \samp{PASV} command, connect to it, and start the transfer
174command. Either way, return the socket for the connection.
175\end{methoddesc}
176
177\begin{methoddesc}{ntransfercmd}{cmd}
178Like \method{transfercmd()}, but returns a tuple of the data
179connection and the expected size of the data. If the expected size
180could not be computed, \code{None} will be returned as the expected
181size.
182\end{methoddesc}
183
Fred Drakefc576191998-04-04 07:15:02 +0000184\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000185Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000186optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000187server directory). Multiple arguments can be used to pass
188non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000189\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000190
Fred Drakefc576191998-04-04 07:15:02 +0000191\begin{methoddesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000192Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000193a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000194(default is the current server directory). Multiple arguments can be
195used to pass non-standard options to the \samp{LIST} command. If the
196last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000197as for \method{retrlines()}.
Fred Drakefc576191998-04-04 07:15:02 +0000198\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000199
Fred Drakefc576191998-04-04 07:15:02 +0000200\begin{methoddesc}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000201Rename file \var{fromname} on the server to \var{toname}.
Fred Drakefc576191998-04-04 07:15:02 +0000202\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000203
Fred Drake4f316941998-04-27 14:54:06 +0000204\begin{methoddesc}{delete}{filename}
205Remove the file named \var{filename} from the server. If successful,
206returns the text of the response, otherwise raises
207\exception{error_perm} on permission errors or \exception{error_reply}
208on other errors.
209\end{methoddesc}
210
Fred Drakefc576191998-04-04 07:15:02 +0000211\begin{methoddesc}{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000212Set the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000213\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000214
Fred Drakefc576191998-04-04 07:15:02 +0000215\begin{methoddesc}{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000216Create a new directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000217\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000218
Fred Drakefc576191998-04-04 07:15:02 +0000219\begin{methoddesc}{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000220Return the pathname of the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000221\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000222
Fred Drake4f316941998-04-27 14:54:06 +0000223\begin{methoddesc}{rmd}{dirname}
224Remove the directory named \var{dirname} on the server.
225\end{methoddesc}
226
227\begin{methoddesc}{size}{filename}
228Request the size of the file named \var{filename} on the server. On
229success, the size of the file is returned as an integer, otherwise
230\code{None} is returned. Note that the \samp{SIZE} command is not
231standardized, but is supported by many common server implementations.
232\end{methoddesc}
233
Fred Drakefc576191998-04-04 07:15:02 +0000234\begin{methoddesc}{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000235Send a \samp{QUIT} command to the server and close the connection.
236This is the ``polite'' way to close a connection, but it may raise an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000237exception of the server reponds with an error to the \samp{QUIT}
Guido van Rossum730d8371998-08-07 17:36:59 +0000238command. This implies a call to the \method{close()} method which
239renders the \class{FTP} instance useless for subsequent calls (see
240below).
Fred Drakefc576191998-04-04 07:15:02 +0000241\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000242
Fred Drakefc576191998-04-04 07:15:02 +0000243\begin{methoddesc}{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000244Close the connection unilaterally. This should not be applied to an
245already closed connection (e.g.\ after a successful call to
Guido van Rossum730d8371998-08-07 17:36:59 +0000246\method{quit()}. After this call the \class{FTP} instance should not
247be used any more (i.e., after a call to \method{close()} or
248\method{quit()} you cannot reopen the connection by issueing another
249\method{login()} method).
Fred Drakefc576191998-04-04 07:15:02 +0000250\end{methoddesc}