blob: 4a5f98c8eb1f07c1e60ae56919836fca35a5c480 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{ftplib} ---
Fred Drake59676671999-03-18 16:08:54 +00002 FTP protocol client}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{ftplib}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{FTP protocol client (requires sockets).}
6
Guido van Rossum86751151995-02-28 17:14:32 +00007
Fred Drake4f316941998-04-27 14:54:06 +00008This module defines the class \class{FTP} and a few related items.
Fred Drake59676671999-03-18 16:08:54 +00009The \class{FTP} class implements the client side of the FTP
10protocol.\indexii{FTP}{protocol} You can use this to write Python
11programs that perform a variety of automated FTP jobs, such as
12mirroring other ftp servers. It is also used by the module
13\refmodule{urllib} to handle URLs that use FTP. For more information
14on FTP (File Transfer Protocol), see Internet \rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000015
Fred Drake6a1eefe1998-03-12 06:04:53 +000016Here's a sample session using the \module{ftplib} module:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000017
Fred Drake19479911998-02-13 06:58:54 +000018\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000019>>> from ftplib import FTP
20>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000021>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000022>>> ftp.retrlines('LIST') # list directory contents
23total 24418
24drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
25dr-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
27 .
28 .
29 .
Fred Drake161edc21998-08-07 17:30:49 +000030>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
31'226 Transfer complete.'
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000032>>> ftp.quit()
Fred Drake19479911998-02-13 06:58:54 +000033\end{verbatim}
Fred Drake6a1eefe1998-03-12 06:04:53 +000034
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000035The module defines the following items:
36
Fred Drakefc576191998-04-04 07:15:02 +000037\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
38 passwd\optional{, acct}}}}}
Fred Drake4f316941998-04-27 14:54:06 +000039Return a new instance of the \class{FTP} class. When
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000040\var{host} is given, the method call \code{connect(\var{host})} is
41made. 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).
Fred Drake6a1eefe1998-03-12 06:04:53 +000044\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000045
46\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000047The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000048instances may raise as a result of problems with the FTP connection
49(as opposed to programming errors made by the caller). This set
50includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000051\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000052\end{datadesc}
53
54\begin{excdesc}{error_reply}
55Exception raised when an unexpected reply is received from the server.
56\end{excdesc}
57
58\begin{excdesc}{error_temp}
59Exception raised when an error code in the range 400--499 is received.
60\end{excdesc}
61
62\begin{excdesc}{error_perm}
63Exception raised when an error code in the range 500--599 is received.
64\end{excdesc}
65
66\begin{excdesc}{error_proto}
67Exception raised when a reply is received from the server that does
68not begin with a digit in the range 1--5.
69\end{excdesc}
70
Fred Drakefc576191998-04-04 07:15:02 +000071
Fred Drakee82f5b31999-03-25 05:04:17 +000072\begin{seealso}
Fred Drake184e8361999-05-11 15:14:15 +000073 \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.}
Fred Drakee82f5b31999-03-25 05:04:17 +000077 \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}
82
83
Fred Drake59676671999-03-18 16:08:54 +000084\subsection{FTP Objects \label{ftp-objects}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000085
Fred Drake33378da1999-05-17 16:35:15 +000086Several methods are available in two flavors: one for handling text
87files and another for binary files. These are named for the command
88which is used followed by \samp{lines} for the text version or
89\samp{binary} for the binary version.
90
Fred Drake6a1eefe1998-03-12 06:04:53 +000091\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000092
Fred Drakefc576191998-04-04 07:15:02 +000093\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000094Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000095debugging output printed. The default, \code{0}, produces no
96debugging output. A value of \code{1} produces a moderate amount of
97debugging output, generally a single line per request. A value of
98\code{2} or higher produces the maximum amount of debugging output,
99logging each line sent and received on the control connection.
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}{connect}{host\optional{, port}}
Fred Drake6a1eefe1998-03-12 06:04:53 +0000103Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000104specified by the FTP protocol specification. It is rarely needed to
105specify a different port number. This function should be called only
106once for each instance; it should not be called at all if a host was
107given when the instance was created. All other methods can only be
108used after a connection has been made.
Fred Drakefc576191998-04-04 07:15:02 +0000109\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000110
Fred Drakefc576191998-04-04 07:15:02 +0000111\begin{methoddesc}{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000112Return the welcome message sent by the server in reply to the initial
113connection. (This message sometimes contains disclaimers or help
114information that may be relevant to the user.)
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}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000118Log in as the given \var{user}. The \var{passwd} and \var{acct}
119parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000120\var{user} is specified, it defaults to \code{'anonymous'}. If
Fred Drake59676671999-03-18 16:08:54 +0000121\var{user} is \code{'anonymous'}, the default \var{passwd} is
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000122\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000123name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000124variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000125\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000126once for each instance, after a connection has been established; it
127should not be called at all if a host and user were given when the
128instance was created. Most FTP commands are only allowed after the
129client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000130\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000131
Fred Drakefc576191998-04-04 07:15:02 +0000132\begin{methoddesc}{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000133Abort a file transfer that is in progress. Using this does not always
134work, but it's worth a try.
Fred Drakefc576191998-04-04 07:15:02 +0000135\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000136
Fred Drakefc576191998-04-04 07:15:02 +0000137\begin{methoddesc}{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000138Send a simple command string to the server and return the response
139string.
Fred Drakefc576191998-04-04 07:15:02 +0000140\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000141
Fred Drakefc576191998-04-04 07:15:02 +0000142\begin{methoddesc}{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000143Send a simple command string to the server and handle the response.
144Return nothing if a response code in the range 200--299 is received.
145Raise an exception otherwise.
Fred Drakefc576191998-04-04 07:15:02 +0000146\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000147
Fred Drakefc576191998-04-04 07:15:02 +0000148\begin{methoddesc}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000149Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000150appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000151The \var{callback} function is called for each block of data received,
152with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000153The 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.
Fred Drakefc576191998-04-04 07:15:02 +0000157\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000158
Fred Drakefc576191998-04-04 07:15:02 +0000159\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000160Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000161\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000162\method{retrbinary()} or a \samp{LIST} command (usually just the string
163\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000164with the trailing CRLF stripped. The default \var{callback} prints
165the line to \code{sys.stdout}.
Fred Drakefc576191998-04-04 07:15:02 +0000166\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000167
Fred Drake59676671999-03-18 16:08:54 +0000168\begin{methoddesc}{set_pasv}{boolean}
169Enable ``passive'' mode if \var{boolean} is true, other disable
170passive mode.
171\end{methoddesc}
172
Fred Drakefc576191998-04-04 07:15:02 +0000173\begin{methoddesc}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000174Store a file in binary transfer mode. \var{command} should be an
175appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000176\var{file} is an open file object which is read until \EOF{} using its
177\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000178data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000179\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000180
Fred Drakefc576191998-04-04 07:15:02 +0000181\begin{methoddesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000182Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000183appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
184read until \EOF{} from the open file object \var{file} using its
185\method{readline()} method to privide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000186\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000187
Fred Drake4f316941998-04-27 14:54:06 +0000188\begin{methoddesc}{transfercmd}{cmd}
189Initiate a transfer over the data connection. If the transfer is
190active, send a \samp{PORT} command and the transfer command specified
191by \var{cmd}, and accept the connection. If the server is passive,
192send a \samp{PASV} command, connect to it, and start the transfer
193command. Either way, return the socket for the connection.
194\end{methoddesc}
195
196\begin{methoddesc}{ntransfercmd}{cmd}
197Like \method{transfercmd()}, but returns a tuple of the data
198connection and the expected size of the data. If the expected size
199could not be computed, \code{None} will be returned as the expected
200size.
201\end{methoddesc}
202
Fred Drakefc576191998-04-04 07:15:02 +0000203\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000204Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000205optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000206server directory). Multiple arguments can be used to pass
207non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000208\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000209
Fred Drakefc576191998-04-04 07:15:02 +0000210\begin{methoddesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000211Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000212a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000213(default is the current server directory). Multiple arguments can be
214used to pass non-standard options to the \samp{LIST} command. If the
215last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000216as for \method{retrlines()}.
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}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000220Rename file \var{fromname} on the server to \var{toname}.
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}{delete}{filename}
224Remove the file named \var{filename} from the server. If successful,
225returns the text of the response, otherwise raises
Fred Drake44297721999-04-22 16:15:34 +0000226\exception{error_perm} on permission errors or
227\exception{error_reply} on other errors.
Fred Drake4f316941998-04-27 14:54:06 +0000228\end{methoddesc}
229
Fred Drakefc576191998-04-04 07:15:02 +0000230\begin{methoddesc}{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000231Set the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000232\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000233
Fred Drakefc576191998-04-04 07:15:02 +0000234\begin{methoddesc}{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000235Create a new directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000236\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000237
Fred Drakefc576191998-04-04 07:15:02 +0000238\begin{methoddesc}{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000239Return the pathname of the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000240\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000241
Fred Drake4f316941998-04-27 14:54:06 +0000242\begin{methoddesc}{rmd}{dirname}
243Remove the directory named \var{dirname} on the server.
244\end{methoddesc}
245
246\begin{methoddesc}{size}{filename}
247Request the size of the file named \var{filename} on the server. On
248success, the size of the file is returned as an integer, otherwise
249\code{None} is returned. Note that the \samp{SIZE} command is not
250standardized, but is supported by many common server implementations.
251\end{methoddesc}
252
Fred Drakefc576191998-04-04 07:15:02 +0000253\begin{methoddesc}{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000254Send a \samp{QUIT} command to the server and close the connection.
255This is the ``polite'' way to close a connection, but it may raise an
Fred Drake44297721999-04-22 16:15:34 +0000256exception of the server reponds with an error to the
257\samp{QUIT} command. This implies a call to the \method{close()}
258method which renders the \class{FTP} instance useless for subsequent
259calls (see below).
Fred Drakefc576191998-04-04 07:15:02 +0000260\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000261
Fred Drakefc576191998-04-04 07:15:02 +0000262\begin{methoddesc}{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000263Close the connection unilaterally. This should not be applied to an
264already closed connection (e.g.\ after a successful call to
Guido van Rossum730d8371998-08-07 17:36:59 +0000265\method{quit()}. After this call the \class{FTP} instance should not
266be used any more (i.e., after a call to \method{close()} or
267\method{quit()} you cannot reopen the connection by issueing another
268\method{login()} method).
Fred Drakefc576191998-04-04 07:15:02 +0000269\end{methoddesc}