blob: 8ba2c61d72f8434b237d0e924bc225a96e23f88d [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{ftplib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-ftplib}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{ftplib}
Fred Drake15bac5d1998-01-07 13:13:42 +00004\indexii{FTP}{protocol}
Guido van Rossum86751151995-02-28 17:14:32 +00005
Guido van Rossum86751151995-02-28 17:14:32 +00006
Fred Drake4f316941998-04-27 14:54:06 +00007This module defines the class \class{FTP} and a few related items.
8The \class{FTP} class implements the client side of the FTP protocol.
9You can use this to write Python programs that perform a variety of
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000010automated FTP jobs, such as mirroring other ftp servers. It is also
Fred Drake6a1eefe1998-03-12 06:04:53 +000011used by the module \module{urllib} to handle URLs that use FTP. For
Fred Drake4f316941998-04-27 14:54:06 +000012more information on FTP (File Transfer Protocol), see Internet
13\rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000014
Fred Drake6a1eefe1998-03-12 06:04:53 +000015Here's a sample session using the \module{ftplib} module:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000016
Fred Drake19479911998-02-13 06:58:54 +000017\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000018>>> from ftplib import FTP
19>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000020>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000021>>> ftp.retrlines('LIST') # list directory contents
22total 24418
23drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
24dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
25-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
26 .
27 .
28 .
29>>> ftp.quit()
Fred Drake19479911998-02-13 06:58:54 +000030\end{verbatim}
Fred Drake6a1eefe1998-03-12 06:04:53 +000031
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000032The module defines the following items:
33
Fred Drakefc576191998-04-04 07:15:02 +000034\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
35 passwd\optional{, acct}}}}}
Fred Drake4f316941998-04-27 14:54:06 +000036Return a new instance of the \class{FTP} class. When
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000037\var{host} is given, the method call \code{connect(\var{host})} is
38made. When \var{user} is given, additionally the method call
39\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
40\var{passwd} and \var{acct} default to the empty string when not given).
Fred Drake6a1eefe1998-03-12 06:04:53 +000041\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000042
43\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000044The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000045instances may raise as a result of problems with the FTP connection
46(as opposed to programming errors made by the caller). This set
47includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000048\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000049\end{datadesc}
50
51\begin{excdesc}{error_reply}
52Exception raised when an unexpected reply is received from the server.
53\end{excdesc}
54
55\begin{excdesc}{error_temp}
56Exception raised when an error code in the range 400--499 is received.
57\end{excdesc}
58
59\begin{excdesc}{error_perm}
60Exception raised when an error code in the range 500--599 is received.
61\end{excdesc}
62
63\begin{excdesc}{error_proto}
64Exception raised when a reply is received from the server that does
65not begin with a digit in the range 1--5.
66\end{excdesc}
67
Fred Drakefc576191998-04-04 07:15:02 +000068
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000069\subsection{FTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000070\label{ftp-objects}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000071
Fred Drake6a1eefe1998-03-12 06:04:53 +000072\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000073
Fred Drakefc576191998-04-04 07:15:02 +000074\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000075Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000076debugging output printed. The default, \code{0}, produces no
77debugging output. A value of \code{1} produces a moderate amount of
78debugging output, generally a single line per request. A value of
79\code{2} or higher produces the maximum amount of debugging output,
80logging each line sent and received on the control connection.
Fred Drakefc576191998-04-04 07:15:02 +000081\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000082
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{methoddesc}{connect}{host\optional{, port}}
Fred Drake6a1eefe1998-03-12 06:04:53 +000084Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000085specified by the FTP protocol specification. It is rarely needed to
86specify a different port number. This function should be called only
87once for each instance; it should not be called at all if a host was
88given when the instance was created. All other methods can only be
89used after a connection has been made.
Fred Drakefc576191998-04-04 07:15:02 +000090\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000091
Fred Drakefc576191998-04-04 07:15:02 +000092\begin{methoddesc}{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000093Return the welcome message sent by the server in reply to the initial
94connection. (This message sometimes contains disclaimers or help
95information that may be relevant to the user.)
Fred Drakefc576191998-04-04 07:15:02 +000096\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000097
Fred Drakefc576191998-04-04 07:15:02 +000098\begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000099Log in as the given \var{user}. The \var{passwd} and \var{acct}
100parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000101\var{user} is specified, it defaults to \code{'anonymous'}. If
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000102\var{user} is \code{anonymous}, the default \var{passwd} is
103\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000104name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000105variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000106\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000107once for each instance, after a connection has been established; it
108should not be called at all if a host and user were given when the
109instance was created. Most FTP commands are only allowed after the
110client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000111\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000112
Fred Drakefc576191998-04-04 07:15:02 +0000113\begin{methoddesc}{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000114Abort a file transfer that is in progress. Using this does not always
115work, but it's worth a try.
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}{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000119Send a simple command string to the server and return the response
120string.
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}{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000124Send a simple command string to the server and handle the response.
125Return nothing if a response code in the range 200--299 is received.
126Raise an exception otherwise.
Fred Drakefc576191998-04-04 07:15:02 +0000127\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000128
Fred Drakefc576191998-04-04 07:15:02 +0000129\begin{methoddesc}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000130Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000131appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000132The \var{callback} function is called for each block of data received,
133with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000134The optional \var{maxblocksize} argument specifies the maximum chunk size to
135read on the low-level socket object created to do the actual transfer
136(which will also be the largest size of the data blocks passed to
137\var{callback}). A reasonable default is chosen.
Fred Drakefc576191998-04-04 07:15:02 +0000138\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000139
Fred Drakefc576191998-04-04 07:15:02 +0000140\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000141Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000142\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000143\method{retrbinary()} or a \samp{LIST} command (usually just the string
144\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000145with the trailing CRLF stripped. The default \var{callback} prints
146the line to \code{sys.stdout}.
Fred Drakefc576191998-04-04 07:15:02 +0000147\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000148
Fred Drakefc576191998-04-04 07:15:02 +0000149\begin{methoddesc}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000150Store a file in binary transfer mode. \var{command} should be an
151appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000152\var{file} is an open file object which is read until \EOF{} using its
153\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000154data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000155\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000156
Fred Drakefc576191998-04-04 07:15:02 +0000157\begin{methoddesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000158Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000159appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
160read until \EOF{} from the open file object \var{file} using its
161\method{readline()} method to privide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000162\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000163
Fred Drake4f316941998-04-27 14:54:06 +0000164\begin{methoddesc}{transfercmd}{cmd}
165Initiate a transfer over the data connection. If the transfer is
166active, send a \samp{PORT} command and the transfer command specified
167by \var{cmd}, and accept the connection. If the server is passive,
168send a \samp{PASV} command, connect to it, and start the transfer
169command. Either way, return the socket for the connection.
170\end{methoddesc}
171
172\begin{methoddesc}{ntransfercmd}{cmd}
173Like \method{transfercmd()}, but returns a tuple of the data
174connection and the expected size of the data. If the expected size
175could not be computed, \code{None} will be returned as the expected
176size.
177\end{methoddesc}
178
Fred Drakefc576191998-04-04 07:15:02 +0000179\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000180Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000181optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000182server directory). Multiple arguments can be used to pass
183non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000184\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000185
Fred Drakefc576191998-04-04 07:15:02 +0000186\begin{methoddesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000187Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000188a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000189(default is the current server directory). Multiple arguments can be
190used to pass non-standard options to the \samp{LIST} command. If the
191last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000192as for \method{retrlines()}.
Fred Drakefc576191998-04-04 07:15:02 +0000193\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000194
Fred Drakefc576191998-04-04 07:15:02 +0000195\begin{methoddesc}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000196Rename file \var{fromname} on the server to \var{toname}.
Fred Drakefc576191998-04-04 07:15:02 +0000197\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000198
Fred Drake4f316941998-04-27 14:54:06 +0000199\begin{methoddesc}{delete}{filename}
200Remove the file named \var{filename} from the server. If successful,
201returns the text of the response, otherwise raises
202\exception{error_perm} on permission errors or \exception{error_reply}
203on other errors.
204\end{methoddesc}
205
Fred Drakefc576191998-04-04 07:15:02 +0000206\begin{methoddesc}{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000207Set the current directory on the server.
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}{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000211Create a new 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}{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000215Return the pathname of the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000216\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000217
Fred Drake4f316941998-04-27 14:54:06 +0000218\begin{methoddesc}{rmd}{dirname}
219Remove the directory named \var{dirname} on the server.
220\end{methoddesc}
221
222\begin{methoddesc}{size}{filename}
223Request the size of the file named \var{filename} on the server. On
224success, the size of the file is returned as an integer, otherwise
225\code{None} is returned. Note that the \samp{SIZE} command is not
226standardized, but is supported by many common server implementations.
227\end{methoddesc}
228
Fred Drakefc576191998-04-04 07:15:02 +0000229\begin{methoddesc}{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000230Send a \samp{QUIT} command to the server and close the connection.
231This is the ``polite'' way to close a connection, but it may raise an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000232exception of the server reponds with an error to the \samp{QUIT}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000233command.
Fred Drakefc576191998-04-04 07:15:02 +0000234\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000235
Fred Drakefc576191998-04-04 07:15:02 +0000236\begin{methoddesc}{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000237Close the connection unilaterally. This should not be applied to an
238already closed connection (e.g.\ after a successful call to
Fred Drake6a1eefe1998-03-12 06:04:53 +0000239\method{quit()}.
Fred Drakefc576191998-04-04 07:15:02 +0000240\end{methoddesc}