blob: 1ce5f9bab36a3ba3473957ce9b23c6282625ebb3 [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
Fred Drakeef338ec2001-12-26 19:48:43 +00007\indexii{FTP}{protocol}
8\index{FTP!\module{ftplib} (standard module)}
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.
Fred Drake59676671999-03-18 16:08:54 +000011The \class{FTP} class implements the client side of the FTP
Fred Drakeef338ec2001-12-26 19:48:43 +000012protocol. You can use this to write Python
Fred Drake59676671999-03-18 16:08:54 +000013programs 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}.
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 Rossumc33e0772001-12-28 20:54:28 +000023>>> ftp.login() # user anonymous, passwd anonymous@
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{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000040 passwd\optional{, acct\optional{, timeout}}}}}}
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).
Guido van Rossumd8faa362007-04-27 19:54:29 +000046The optional \var{timeout} parameter specifies a timeout in seconds for the
47connection attempt (if is not specified, or passed as None, the global
48default timeout setting will be used).
Guido van Rossumcd16bf62007-06-13 18:07:49 +000049\versionchanged[\var{timeout} was added]{2.6}
Fred Drake6a1eefe1998-03-12 06:04:53 +000050\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000051
52\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000053The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000054instances may raise as a result of problems with the FTP connection
55(as opposed to programming errors made by the caller). This set
56includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000057\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000058\end{datadesc}
59
60\begin{excdesc}{error_reply}
61Exception raised when an unexpected reply is received from the server.
62\end{excdesc}
63
64\begin{excdesc}{error_temp}
65Exception raised when an error code in the range 400--499 is received.
66\end{excdesc}
67
68\begin{excdesc}{error_perm}
69Exception raised when an error code in the range 500--599 is received.
70\end{excdesc}
71
72\begin{excdesc}{error_proto}
73Exception raised when a reply is received from the server that does
74not begin with a digit in the range 1--5.
75\end{excdesc}
76
Fred Drakefc576191998-04-04 07:15:02 +000077
Fred Drakee82f5b31999-03-25 05:04:17 +000078\begin{seealso}
Fred Drake184e8361999-05-11 15:14:15 +000079 \seemodule{netrc}{Parser for the \file{.netrc} file format. The file
80 \file{.netrc} is typically used by FTP clients to
81 load user authentication information before prompting
82 the user.}
Fred Drakee82f5b31999-03-25 05:04:17 +000083 \seetext{The file \file{Tools/scripts/ftpmirror.py}\index{ftpmirror.py}
84 in the Python source distribution is a script that can mirror
85 FTP sites, or portions thereof, using the \module{ftplib} module.
86 It can be used as an extended example that applies this module.}
87\end{seealso}
88
89
Fred Drake59676671999-03-18 16:08:54 +000090\subsection{FTP Objects \label{ftp-objects}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000091
Fred Drake33378da1999-05-17 16:35:15 +000092Several methods are available in two flavors: one for handling text
93files and another for binary files. These are named for the command
94which is used followed by \samp{lines} for the text version or
95\samp{binary} for the binary version.
96
Fred Drake6a1eefe1998-03-12 06:04:53 +000097\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000098
Guido van Rossumd8faa362007-04-27 19:54:29 +000099\begin{methoddesc}[FTP]{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000100Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +0000101debugging output printed. The default, \code{0}, produces no
102debugging output. A value of \code{1} produces a moderate amount of
103debugging output, generally a single line per request. A value of
104\code{2} or higher produces the maximum amount of debugging output,
105logging each line sent and received on the control connection.
Fred Drakefc576191998-04-04 07:15:02 +0000106\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000107
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108\begin{methoddesc}[FTP]{connect}{host\optional{, port\optional{, timeout}}}
Fred Drake6a1eefe1998-03-12 06:04:53 +0000109Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000110specified by the FTP protocol specification. It is rarely needed to
111specify a different port number. This function should be called only
112once for each instance; it should not be called at all if a host was
113given when the instance was created. All other methods can only be
114used after a connection has been made.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115
116The optional \var{timeout} parameter specifies a timeout in seconds for
117the connection attempt. If is not specified, or passed as None, the
118object timeout is used (the timeout that you passed when instantiating the
119class); if the object timeout is also None, the global default timeout
120setting will be used.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000121
122\versionchanged[\var{timeout} was added]{2.6}
Fred Drakefc576191998-04-04 07:15:02 +0000123\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000124
Guido van Rossumd8faa362007-04-27 19:54:29 +0000125\begin{methoddesc}[FTP]{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000126Return the welcome message sent by the server in reply to the initial
127connection. (This message sometimes contains disclaimers or help
128information that may be relevant to the user.)
Fred Drakefc576191998-04-04 07:15:02 +0000129\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000130
Guido van Rossumd8faa362007-04-27 19:54:29 +0000131\begin{methoddesc}[FTP]{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000132Log in as the given \var{user}. The \var{passwd} and \var{acct}
133parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000134\var{user} is specified, it defaults to \code{'anonymous'}. If
Fred Drake59676671999-03-18 16:08:54 +0000135\var{user} is \code{'anonymous'}, the default \var{passwd} is
Guido van Rossumc33e0772001-12-28 20:54:28 +0000136\code{'anonymous@'}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000137once for each instance, after a connection has been established; it
138should not be called at all if a host and user were given when the
139instance was created. Most FTP commands are only allowed after the
140client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000141\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000142
Guido van Rossumd8faa362007-04-27 19:54:29 +0000143\begin{methoddesc}[FTP]{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000144Abort a file transfer that is in progress. Using this does not always
145work, but it's worth a try.
Fred Drakefc576191998-04-04 07:15:02 +0000146\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148\begin{methoddesc}[FTP]{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000149Send a simple command string to the server and return the response
150string.
Fred Drakefc576191998-04-04 07:15:02 +0000151\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000152
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153\begin{methoddesc}[FTP]{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000154Send a simple command string to the server and handle the response.
155Return nothing if a response code in the range 200--299 is received.
156Raise an exception otherwise.
Fred Drakefc576191998-04-04 07:15:02 +0000157\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000158
Guido van Rossumd8faa362007-04-27 19:54:29 +0000159\begin{methoddesc}[FTP]{retrbinary}{command,
Barry Warsaw21fbd542000-09-01 06:32:32 +0000160 callback\optional{, maxblocksize\optional{, rest}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000161Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake91f2f262001-07-06 19:28:48 +0000162appropriate \samp{RETR} command: \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000163The \var{callback} function is called for each block of data received,
164with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000165The optional \var{maxblocksize} argument specifies the maximum chunk size to
166read on the low-level socket object created to do the actual transfer
167(which will also be the largest size of the data blocks passed to
Barry Warsaw21fbd542000-09-01 06:32:32 +0000168\var{callback}). A reasonable default is chosen. \var{rest} means the
169same thing as in the \method{transfercmd()} method.
Fred Drakefc576191998-04-04 07:15:02 +0000170\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000171
Guido van Rossumd8faa362007-04-27 19:54:29 +0000172\begin{methoddesc}[FTP]{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000173Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000174\var{command} should be an appropriate \samp{RETR} command (see
Raymond Hettinger52136a82003-05-10 03:35:37 +0000175\method{retrbinary()}) or a \samp{LIST} command (usually just the string
Fred Drake6a1eefe1998-03-12 06:04:53 +0000176\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000177with the trailing CRLF stripped. The default \var{callback} prints
178the line to \code{sys.stdout}.
Fred Drakefc576191998-04-04 07:15:02 +0000179\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000180
Guido van Rossumd8faa362007-04-27 19:54:29 +0000181\begin{methoddesc}[FTP]{set_pasv}{boolean}
Fred Drake59676671999-03-18 16:08:54 +0000182Enable ``passive'' mode if \var{boolean} is true, other disable
Guido van Rossum97d3b932001-01-15 16:37:05 +0000183passive mode. (In Python 2.0 and before, passive mode was off by
184default; in Python 2.1 and later, it is on by default.)
Fred Drake59676671999-03-18 16:08:54 +0000185\end{methoddesc}
186
Guido van Rossumd8faa362007-04-27 19:54:29 +0000187\begin{methoddesc}[FTP]{storbinary}{command, file\optional{, blocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000188Store a file in binary transfer mode. \var{command} should be an
Fred Drake91f2f262001-07-06 19:28:48 +0000189appropriate \samp{STOR} command: \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000190\var{file} is an open file object which is read until \EOF{} using its
191\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumbf416fa2001-02-15 13:53:40 +0000192data to be stored. The \var{blocksize} argument defaults to 8192.
193\versionchanged[default for \var{blocksize} added]{2.1}
Fred Drakefc576191998-04-04 07:15:02 +0000194\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000195
Guido van Rossumd8faa362007-04-27 19:54:29 +0000196\begin{methoddesc}[FTP]{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000197Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000198appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
199read until \EOF{} from the open file object \var{file} using its
Fred Drake5d48e451999-07-20 13:21:42 +0000200\method{readline()} method to provide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000201\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000202
Guido van Rossumd8faa362007-04-27 19:54:29 +0000203\begin{methoddesc}[FTP]{transfercmd}{cmd\optional{, rest}}
Fred Drake4f316941998-04-27 14:54:06 +0000204Initiate a transfer over the data connection. If the transfer is
Martin v. Löwisc9908c42001-08-04 22:22:45 +0000205active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified
Fred Drake4f316941998-04-27 14:54:06 +0000206by \var{cmd}, and accept the connection. If the server is passive,
Martin v. Löwisc9908c42001-08-04 22:22:45 +0000207send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
Fred Drake4f316941998-04-27 14:54:06 +0000208command. Either way, return the socket for the connection.
Barry Warsaw21fbd542000-09-01 06:32:32 +0000209
210If optional \var{rest} is given, a \samp{REST} command is
211sent to the server, passing \var{rest} as an argument. \var{rest} is
212usually a byte offset into the requested file, telling the server to
213restart sending the file's bytes at the requested offset, skipping
214over the initial bytes. Note however that RFC
215959 requires only that \var{rest} be a string containing characters
216in the printable range from ASCII code 33 to ASCII code 126. The
217\method{transfercmd()} method, therefore, converts
218\var{rest} to a string, but no check is
219performed on the string's contents. If the server does
220not recognize the \samp{REST} command, an
221\exception{error_reply} exception will be raised. If this happens,
222simply call \method{transfercmd()} without a \var{rest} argument.
Fred Drake4f316941998-04-27 14:54:06 +0000223\end{methoddesc}
224
Guido van Rossumd8faa362007-04-27 19:54:29 +0000225\begin{methoddesc}[FTP]{ntransfercmd}{cmd\optional{, rest}}
Fred Drake4f316941998-04-27 14:54:06 +0000226Like \method{transfercmd()}, but returns a tuple of the data
227connection and the expected size of the data. If the expected size
228could not be computed, \code{None} will be returned as the expected
Barry Warsaw21fbd542000-09-01 06:32:32 +0000229size. \var{cmd} and \var{rest} means the same thing as in
230\method{transfercmd()}.
Fred Drake4f316941998-04-27 14:54:06 +0000231\end{methoddesc}
232
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233\begin{methoddesc}[FTP]{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000234Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000235optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000236server directory). Multiple arguments can be used to pass
237non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000238\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000239
Guido van Rossumd8faa362007-04-27 19:54:29 +0000240\begin{methoddesc}[FTP]{dir}{argument\optional{, \ldots}}
Fred Drake15089701999-07-07 13:36:22 +0000241Produce a directory listing as returned by the \samp{LIST} command,
242printing it to standard output. The optional \var{argument} is a
243directory to list (default is the current server directory). Multiple
244arguments can be used to pass non-standard options to the \samp{LIST}
245command. If the last argument is a function, it is used as a
246\var{callback} function as for \method{retrlines()}; the default
247prints to \code{sys.stdout}. This method returns \code{None}.
Fred Drakefc576191998-04-04 07:15:02 +0000248\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000249
Guido van Rossumd8faa362007-04-27 19:54:29 +0000250\begin{methoddesc}[FTP]{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000251Rename file \var{fromname} on the server to \var{toname}.
Fred Drakefc576191998-04-04 07:15:02 +0000252\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000253
Guido van Rossumd8faa362007-04-27 19:54:29 +0000254\begin{methoddesc}[FTP]{delete}{filename}
Fred Drake4f316941998-04-27 14:54:06 +0000255Remove the file named \var{filename} from the server. If successful,
256returns the text of the response, otherwise raises
Fred Drake44297721999-04-22 16:15:34 +0000257\exception{error_perm} on permission errors or
258\exception{error_reply} on other errors.
Fred Drake4f316941998-04-27 14:54:06 +0000259\end{methoddesc}
260
Guido van Rossumd8faa362007-04-27 19:54:29 +0000261\begin{methoddesc}[FTP]{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000262Set the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000263\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000264
Guido van Rossumd8faa362007-04-27 19:54:29 +0000265\begin{methoddesc}[FTP]{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000266Create a new directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000267\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000268
Guido van Rossumd8faa362007-04-27 19:54:29 +0000269\begin{methoddesc}[FTP]{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000270Return the pathname of the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000271\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000272
Guido van Rossumd8faa362007-04-27 19:54:29 +0000273\begin{methoddesc}[FTP]{rmd}{dirname}
Fred Drake4f316941998-04-27 14:54:06 +0000274Remove the directory named \var{dirname} on the server.
275\end{methoddesc}
276
Guido van Rossumd8faa362007-04-27 19:54:29 +0000277\begin{methoddesc}[FTP]{size}{filename}
Fred Drake4f316941998-04-27 14:54:06 +0000278Request the size of the file named \var{filename} on the server. On
279success, the size of the file is returned as an integer, otherwise
280\code{None} is returned. Note that the \samp{SIZE} command is not
281standardized, but is supported by many common server implementations.
282\end{methoddesc}
283
Guido van Rossumd8faa362007-04-27 19:54:29 +0000284\begin{methoddesc}[FTP]{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000285Send a \samp{QUIT} command to the server and close the connection.
286This is the ``polite'' way to close a connection, but it may raise an
Fred Drake44297721999-04-22 16:15:34 +0000287exception of the server reponds with an error to the
288\samp{QUIT} command. This implies a call to the \method{close()}
289method which renders the \class{FTP} instance useless for subsequent
290calls (see below).
Fred Drakefc576191998-04-04 07:15:02 +0000291\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000292
Guido van Rossumd8faa362007-04-27 19:54:29 +0000293\begin{methoddesc}[FTP]{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000294Close the connection unilaterally. This should not be applied to an
Raymond Hettinger52136a82003-05-10 03:35:37 +0000295already closed connection such as after a successful call to
Guido van Rossum730d8371998-08-07 17:36:59 +0000296\method{quit()}. After this call the \class{FTP} instance should not
Fred Drake91f2f262001-07-06 19:28:48 +0000297be used any more (after a call to \method{close()} or
Thomas Woutersf8316632000-07-16 19:01:10 +0000298\method{quit()} you cannot reopen the connection by issuing another
Guido van Rossum730d8371998-08-07 17:36:59 +0000299\method{login()} method).
Fred Drakefc576191998-04-04 07:15:02 +0000300\end{methoddesc}