blob: dfbaa2be071d5a76d4c1a6953c41b75b426a0f41 [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}
Guido van Rossum86751151995-02-28 17:14:32 +00004
5\renewcommand{\indexsubitem}{(in module ftplib)}
6
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00007This module defines the class \code{FTP} and a few related items. The
8\code{FTP} class implements the client side of the FTP protocol. You
9can use this to write Python programs that perform a variety of
10automated FTP jobs, such as mirroring other ftp servers. It is also
Guido van Rossum0bbbea11995-08-10 14:21:11 +000011used by the module \code{urllib} to handle URLs that use FTP. For
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000012more information on FTP (File Transfer Protocol), see Internet RFC
13959.
14
15Here's a sample session using the \code{ftplib} module:
16
Guido van Rossume47da0a1997-07-17 16:34:52 +000017\bcode\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()
Guido van Rossume47da0a1997-07-17 16:34:52 +000030\end{verbatim}\ecode
31%
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000032The module defines the following items:
33
34\begin{funcdesc}{FTP}{\optional{host\optional{\, user\, passwd\, acct}}}
35Return 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).
40\end{funcdesc}
41
42\begin{datadesc}{all_errors}
43The set of all exceptions (as a tuple) that methods of \code{FTP}
44instances 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
47\code{socket.error} and \code{IOError}.
48\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
67\subsection{FTP Objects}
68
69FTP instances have the following methods:
70
71\renewcommand{\indexsubitem}{(FTP object method)}
72
73\begin{funcdesc}{set_debuglevel}{level}
74Set the instance's debugging level. This controls the amount of
75debugging output printed. The default, 0, produces no debugging
76output. A value of 1 produces a moderate amount of debugging output,
77generally a single line per request. A value of 2 or higher produces
78the maximum amount of debugging output, logging each line sent and
79received on the control connection.
80\end{funcdesc}
81
82\begin{funcdesc}{connect}{host\optional{\, port}}
83Connect to the given host and port. The default port number is 21, as
84specified 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.
89\end{funcdesc}
90
91\begin{funcdesc}{getwelcome}{}
92Return 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.)
95\end{funcdesc}
96
97\begin{funcdesc}{login}{\optional{user\optional{\, passwd\optional{\, acct}}}}
98Log in as the given \var{user}. The \var{passwd} and \var{acct}
99parameters are optional and default to the empty string. If no
100\var{user} is specified, it defaults to \samp{anonymous}. If
101\var{user} is \code{anonymous}, the default \var{passwd} is
102\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
103name (glanced from the \samp{LOGNAME} or \samp{USER} environment
104variable) and \var{host} is the hostname as returned by
105\code{socket.gethostname()}. This function should be called only
106once 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.
110\end{funcdesc}
111
112\begin{funcdesc}{abort}{}
113Abort a file transfer that is in progress. Using this does not always
114work, but it's worth a try.
115\end{funcdesc}
116
117\begin{funcdesc}{sendcmd}{command}
118Send a simple command string to the server and return the response
119string.
120\end{funcdesc}
121
122\begin{funcdesc}{voidcmd}{command}
123Send 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.
126\end{funcdesc}
127
128\begin{funcdesc}{retrbinary}{command\, callback\, maxblocksize}
129Retrieve a file in binary transfer mode. \var{command} should be an
130appropriate \samp{RETR} command, i.e.\ \code{"RETR \var{filename}"}.
131The \var{callback} function is called for each block of data received,
132with a single string argument giving the data block.
133The \var{maxblocksize} argument specifies the maximum block size
134(which may not be the actual size of the data blocks passed to
135\var{callback}).
136\end{funcdesc}
137
138\begin{funcdesc}{retrlines}{command\optional{\, callback}}
139Retrieve 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
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000141\code{retrbinary()} or a \samp{LIST} command (usually just the string
142\code{"LIST"}). The \var{callback} function is called for each line,
143with the trailing CRLF stripped. The default \var{callback} prints
144the line to \code{sys.stdout}.
145\end{funcdesc}
146
147\begin{funcdesc}{storbinary}{command\, file\, blocksize}
148Store a file in binary transfer mode. \var{command} should be an
149appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
150\var{file} is an open file object which is read until EOF using its
151\code{read()} method in blocks of size \var{blocksize} to provide the
152data to be stored.
153\end{funcdesc}
154
155\begin{funcdesc}{storlines}{command\, file}
156Store a file in \ASCII{} transfer mode. \var{command} should be an
157appropriate \samp{STOR} command (see \code{storbinary()}). Lines are
158read until EOF from the open file object \var{file} using its
159\code{readline()} method to privide the data to be stored.
160\end{funcdesc}
161
162\begin{funcdesc}{nlst}{argument\optional{\, \ldots}}
163Return 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
169\begin{funcdesc}{dir}{argument\optional{\, \ldots}}
170Return 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
175as for \code{retrlines()}.
176\end{funcdesc}
177
178\begin{funcdesc}{rename}{fromname\, toname}
179Rename 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
197exception of the server reponds with an error to the \code{QUIT}
198command.
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
204\code{quit()}.
205\end{funcdesc}