blob: cda8b66fd4d53120a6b688e0a6f036a8c1593579 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{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 Drake6a1eefe1998-03-12 06:04:53 +000033\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{, passwd\optional{, acct}}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000034Return a new instance of the \code{FTP} class. When
35\var{host} is given, the method call \code{connect(\var{host})} is
36made. When \var{user} is given, additionally the method call
37\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
38\var{passwd} and \var{acct} default to the empty string when not given).
Fred Drake6a1eefe1998-03-12 06:04:53 +000039\end{classdesc}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000040
41\begin{datadesc}{all_errors}
Fred Drake6a1eefe1998-03-12 06:04:53 +000042The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000043instances may raise as a result of problems with the FTP connection
44(as opposed to programming errors made by the caller). This set
45includes the four exceptions listed below as well as
Fred Drake6a1eefe1998-03-12 06:04:53 +000046\exception{socket.error} and \exception{IOError}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000047\end{datadesc}
48
49\begin{excdesc}{error_reply}
50Exception raised when an unexpected reply is received from the server.
51\end{excdesc}
52
53\begin{excdesc}{error_temp}
54Exception raised when an error code in the range 400--499 is received.
55\end{excdesc}
56
57\begin{excdesc}{error_perm}
58Exception raised when an error code in the range 500--599 is received.
59\end{excdesc}
60
61\begin{excdesc}{error_proto}
62Exception raised when a reply is received from the server that does
63not begin with a digit in the range 1--5.
64\end{excdesc}
65
66\subsection{FTP Objects}
67
Fred Drake6a1eefe1998-03-12 06:04:53 +000068\class{FTP} instances have the following methods:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000069
Fred Drake6a1eefe1998-03-12 06:04:53 +000070\setindexsubitem{(FTP method)}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000071
72\begin{funcdesc}{set_debuglevel}{level}
73Set the instance's debugging level. This controls the amount of
Fred Drake6a1eefe1998-03-12 06:04:53 +000074debugging output printed. The default, \code{0}, produces no
75debugging output. A value of \code{1} produces a moderate amount of
76debugging output, generally a single line per request. A value of
77\code{2} or higher produces the maximum amount of debugging output,
78logging each line sent and received on the control connection.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000079\end{funcdesc}
80
Fred Drake6a1eefe1998-03-12 06:04:53 +000081\begin{funcdesc}{connect}{host\optional{, port}}
82Connect to the given host and port. The default port number is \code{21}, as
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000083specified by the FTP protocol specification. It is rarely needed to
84specify a different port number. This function should be called only
85once for each instance; it should not be called at all if a host was
86given when the instance was created. All other methods can only be
87used after a connection has been made.
88\end{funcdesc}
89
90\begin{funcdesc}{getwelcome}{}
91Return the welcome message sent by the server in reply to the initial
92connection. (This message sometimes contains disclaimers or help
93information that may be relevant to the user.)
94\end{funcdesc}
95
Fred Drake6a1eefe1998-03-12 06:04:53 +000096\begin{funcdesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000097Log in as the given \var{user}. The \var{passwd} and \var{acct}
98parameters are optional and default to the empty string. If no
Fred Drake6a1eefe1998-03-12 06:04:53 +000099\var{user} is specified, it defaults to \code{'anonymous'}. If
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000100\var{user} is \code{anonymous}, the default \var{passwd} is
101\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake6a1eefe1998-03-12 06:04:53 +0000102name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000103variable) and \var{host} is the hostname as returned by
Fred Drake6a1eefe1998-03-12 06:04:53 +0000104\function{socket.gethostname()}. This function should be called only
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000105once for each instance, after a connection has been established; it
106should not be called at all if a host and user were given when the
107instance was created. Most FTP commands are only allowed after the
108client has logged in.
109\end{funcdesc}
110
111\begin{funcdesc}{abort}{}
112Abort a file transfer that is in progress. Using this does not always
113work, but it's worth a try.
114\end{funcdesc}
115
116\begin{funcdesc}{sendcmd}{command}
117Send a simple command string to the server and return the response
118string.
119\end{funcdesc}
120
121\begin{funcdesc}{voidcmd}{command}
122Send a simple command string to the server and handle the response.
123Return nothing if a response code in the range 200--299 is received.
124Raise an exception otherwise.
125\end{funcdesc}
126
Fred Drake6a1eefe1998-03-12 06:04:53 +0000127\begin{funcdesc}{retrbinary}{command, callback\optional{, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000128Retrieve a file in binary transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000129appropriate \samp{RETR} command, i.e.\ \code{'RETR \var{filename}'}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000130The \var{callback} function is called for each block of data received,
131with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000132The optional \var{maxblocksize} argument specifies the maximum chunk size to
133read on the low-level socket object created to do the actual transfer
134(which will also be the largest size of the data blocks passed to
135\var{callback}). A reasonable default is chosen.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000136\end{funcdesc}
137
Fred Drake6a1eefe1998-03-12 06:04:53 +0000138\begin{funcdesc}{retrlines}{command\optional{, callback}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000139Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000140\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake6a1eefe1998-03-12 06:04:53 +0000141\method{retrbinary()} or a \samp{LIST} command (usually just the string
142\code{'LIST'}). The \var{callback} function is called for each line,
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000143with the trailing CRLF stripped. The default \var{callback} prints
144the line to \code{sys.stdout}.
145\end{funcdesc}
146
Fred Drake6a1eefe1998-03-12 06:04:53 +0000147\begin{funcdesc}{storbinary}{command, file, blocksize}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000148Store a file in binary transfer mode. \var{command} should be an
149appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
Fred Drake6a1eefe1998-03-12 06:04:53 +0000150\var{file} is an open file object which is read until \EOF{} using its
151\method{read()} method in blocks of size \var{blocksize} to provide the
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000152data to be stored.
153\end{funcdesc}
154
Fred Drake6a1eefe1998-03-12 06:04:53 +0000155\begin{funcdesc}{storlines}{command, file}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000156Store a file in \ASCII{} transfer mode. \var{command} should be an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000157appropriate \samp{STOR} command (see \method{storbinary()}). Lines are
158read until \EOF{} from the open file object \var{file} using its
159\method{readline()} method to privide the data to be stored.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000160\end{funcdesc}
161
Fred Drake6a1eefe1998-03-12 06:04:53 +0000162\begin{funcdesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000163Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000164optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000165server directory). Multiple arguments can be used to pass
166non-standard options to the \samp{NLST} command.
167\end{funcdesc}
168
Fred Drake6a1eefe1998-03-12 06:04:53 +0000169\begin{funcdesc}{dir}{argument\optional{, \ldots}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000170Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000171a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000172(default is the current server directory). Multiple arguments can be
173used to pass non-standard options to the \samp{LIST} command. If the
174last argument is a function, it is used as a \var{callback} function
Fred Drake6a1eefe1998-03-12 06:04:53 +0000175as for \method{retrlines()}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000176\end{funcdesc}
177
Fred Drake6a1eefe1998-03-12 06:04:53 +0000178\begin{funcdesc}{rename}{fromname, toname}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000179Rename file \var{fromname} on the server to \var{toname}.
180\end{funcdesc}
181
182\begin{funcdesc}{cwd}{pathname}
183Set the current directory on the server.
184\end{funcdesc}
185
186\begin{funcdesc}{mkd}{pathname}
187Create a new directory on the server.
188\end{funcdesc}
189
190\begin{funcdesc}{pwd}{}
191Return the pathname of the current directory on the server.
192\end{funcdesc}
193
194\begin{funcdesc}{quit}{}
195Send a \samp{QUIT} command to the server and close the connection.
196This is the ``polite'' way to close a connection, but it may raise an
Fred Drake6a1eefe1998-03-12 06:04:53 +0000197exception of the server reponds with an error to the \samp{QUIT}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000198command.
199\end{funcdesc}
200
201\begin{funcdesc}{close}{}
202Close the connection unilaterally. This should not be applied to an
203already closed connection (e.g.\ after a successful call to
Fred Drake6a1eefe1998-03-12 06:04:53 +0000204\method{quit()}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000205\end{funcdesc}