blob: 7f0aedaa2f5b84aaed2f83cc8ec4f424e241f63f [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 Drake6a1eefe1998-03-12 06:04:53 +00007This module defines the class \class{FTP} and a few related items. The
8\class{FTP} class implements the client side of the FTP protocol. You
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00009can use this to write Python programs that perform a variety of
10automated 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 Drakec5891241998-02-09 19:16:20 +000012more information on FTP (File Transfer Protocol), see Internet \rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000013
Fred Drake6a1eefe1998-03-12 06:04:53 +000014Here's a sample session using the \module{ftplib} module:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000015
Fred Drake19479911998-02-13 06:58:54 +000016\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000017>>> from ftplib import FTP
18>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000019>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000020>>> ftp.retrlines('LIST') # list directory contents
21total 24418
22drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
23dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
24-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
25 .
26 .
27 .
28>>> ftp.quit()
Fred Drake19479911998-02-13 06:58:54 +000029\end{verbatim}
Fred Drake6a1eefe1998-03-12 06:04:53 +000030
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000031The module defines the following items:
32
Fred Drakefc576191998-04-04 07:15:02 +000033\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
34 passwd\optional{, acct}}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000035Return a new instance of the \code{FTP} class. When
36\var{host} is given, the method call \code{connect(\var{host})} is
37made. When \var{user} is given, additionally the method call
38\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
39\var{passwd} and \var{acct} default to the empty string when not given).
Fred Drake6a1eefe1998-03-12 06:04:53 +000040\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000041
42\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000043The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000044instances may raise as a result of problems with the FTP connection
45(as opposed to programming errors made by the caller). This set
46includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000047\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000048\end{datadesc}
49
50\begin{excdesc}{error_reply}
51Exception raised when an unexpected reply is received from the server.
52\end{excdesc}
53
54\begin{excdesc}{error_temp}
55Exception raised when an error code in the range 400--499 is received.
56\end{excdesc}
57
58\begin{excdesc}{error_perm}
59Exception raised when an error code in the range 500--599 is received.
60\end{excdesc}
61
62\begin{excdesc}{error_proto}
63Exception raised when a reply is received from the server that does
64not begin with a digit in the range 1--5.
65\end{excdesc}
66
Fred Drakefc576191998-04-04 07:15:02 +000067
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000068\subsection{FTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000069\label{ftp-objects}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000070
Fred Drake6a1eefe1998-03-12 06:04:53 +000071\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000072
Fred Drakefc576191998-04-04 07:15:02 +000073\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000074Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000075debugging output printed. The default, \code{0}, produces no
76debugging output. A value of \code{1} produces a moderate amount of
77debugging output, generally a single line per request. A value of
78\code{2} or higher produces the maximum amount of debugging output,
79logging each line sent and received on the control connection.
Fred Drakefc576191998-04-04 07:15:02 +000080\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000081
Fred Drakefc576191998-04-04 07:15:02 +000082\begin{methoddesc}{connect}{host\optional{, port}}
Fred Drake6a1eefe1998-03-12 06:04:53 +000083Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000084specified by the FTP protocol specification. It is rarely needed to
85specify a different port number. This function should be called only
86once for each instance; it should not be called at all if a host was
87given when the instance was created. All other methods can only be
88used after a connection has been made.
Fred Drakefc576191998-04-04 07:15:02 +000089\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000090
Fred Drakefc576191998-04-04 07:15:02 +000091\begin{methoddesc}{getwelcome}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000092Return the welcome message sent by the server in reply to the initial
93connection. (This message sometimes contains disclaimers or help
94information that may be relevant to the user.)
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}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000098Log in as the given \var{user}. The \var{passwd} and \var{acct}
99parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +0000100\var{user} is specified, it defaults to \code{'anonymous'}. If
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000101\var{user} is \code{anonymous}, the default \var{passwd} is
102\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000103name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000104variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000105\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000106once for each instance, after a connection has been established; it
107should not be called at all if a host and user were given when the
108instance was created. Most FTP commands are only allowed after the
109client has logged in.
Fred Drakefc576191998-04-04 07:15:02 +0000110\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000111
Fred Drakefc576191998-04-04 07:15:02 +0000112\begin{methoddesc}{abort}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000113Abort a file transfer that is in progress. Using this does not always
114work, but it's worth a try.
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}{sendcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000118Send a simple command string to the server and return the response
119string.
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}{voidcmd}{command}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000123Send a simple command string to the server and handle the response.
124Return nothing if a response code in the range 200--299 is received.
125Raise an exception otherwise.
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}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000129Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000130appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000131The \var{callback} function is called for each block of data received,
132with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000133The optional \var{maxblocksize} argument specifies the maximum chunk size to
134read on the low-level socket object created to do the actual transfer
135(which will also be the largest size of the data blocks passed to
136\var{callback}). A reasonable default is chosen.
Fred Drakefc576191998-04-04 07:15:02 +0000137\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000138
Fred Drakefc576191998-04-04 07:15:02 +0000139\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000140Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000141\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000142\method{retrbinary()} or a \samp{LIST} command (usually just the string
143\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000144with the trailing CRLF stripped. The default \var{callback} prints
145the line to \code{sys.stdout}.
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}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000149Store a file in binary transfer mode. \var{command} should be an
150appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000151\var{file} is an open file object which is read until \EOF{} using its
152\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000153data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000154\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000155
Fred Drakefc576191998-04-04 07:15:02 +0000156\begin{methoddesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000157Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000158appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
159read until \EOF{} from the open file object \var{file} using its
160\method{readline()} method to privide the data to be stored.
Fred Drakefc576191998-04-04 07:15:02 +0000161\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000162
Fred Drakefc576191998-04-04 07:15:02 +0000163\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000164Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000165optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000166server directory). Multiple arguments can be used to pass
167non-standard options to the \samp{NLST} command.
Fred Drakefc576191998-04-04 07:15:02 +0000168\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000169
Fred Drakefc576191998-04-04 07:15:02 +0000170\begin{methoddesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000171Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000172a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000173(default is the current server directory). Multiple arguments can be
174used to pass non-standard options to the \samp{LIST} command. If the
175last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000176as for \method{retrlines()}.
Fred Drakefc576191998-04-04 07:15:02 +0000177\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000178
Fred Drakefc576191998-04-04 07:15:02 +0000179\begin{methoddesc}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000180Rename file \var{fromname} on the server to \var{toname}.
Fred Drakefc576191998-04-04 07:15:02 +0000181\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}{cwd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000184Set the current directory on the server.
Fred Drakefc576191998-04-04 07:15:02 +0000185\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000186
Fred Drakefc576191998-04-04 07:15:02 +0000187\begin{methoddesc}{mkd}{pathname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000188Create a new directory on the server.
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}{pwd}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000192Return the pathname of the current directory on the server.
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}{quit}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000196Send a \samp{QUIT} command to the server and close the connection.
197This is the ``polite'' way to close a connection, but it may raise an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000198exception of the server reponds with an error to the \samp{QUIT}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000199command.
Fred Drakefc576191998-04-04 07:15:02 +0000200\end{methoddesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000201
Fred Drakefc576191998-04-04 07:15:02 +0000202\begin{methoddesc}{close}{}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000203Close the connection unilaterally. This should not be applied to an
204already closed connection (e.g.\ after a successful call to
Fred Drake6a1eefe1998-03-12 06:04:53 +0000205\method{quit()}.
Fred Drakefc576191998-04-04 07:15:02 +0000206\end{methoddesc}