blob: 10bacbfc0ee94f4e558f32da9affb0c1cca31fae [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}
Fred Drakec5891241998-02-09 19:16:20 +00005\rfcindex{959}
Guido van Rossum86751151995-02-28 17:14:32 +00006
7\renewcommand{\indexsubitem}{(in module ftplib)}
8
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00009This module defines the class \code{FTP} and a few related items. The
10\code{FTP} class implements the client side of the FTP protocol. You
11can use this to write Python programs that perform a variety of
12automated FTP jobs, such as mirroring other ftp servers. It is also
Guido van Rossum0bbbea11995-08-10 14:21:11 +000013used by the module \code{urllib} to handle URLs that use FTP. For
Fred Drakec5891241998-02-09 19:16:20 +000014more information on FTP (File Transfer Protocol), see Internet \rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000015
16Here's a sample session using the \code{ftplib} module:
17
Guido van Rossume47da0a1997-07-17 16:34:52 +000018\bcode\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000019>>> from ftplib import FTP
20>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000021>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000022>>> ftp.retrlines('LIST') # list directory contents
23total 24418
24drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
25dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
26-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
27 .
28 .
29 .
30>>> ftp.quit()
Guido van Rossume47da0a1997-07-17 16:34:52 +000031\end{verbatim}\ecode
32%
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000033The module defines the following items:
34
35\begin{funcdesc}{FTP}{\optional{host\optional{\, user\, passwd\, acct}}}
36Return a new instance of the \code{FTP} class. When
37\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).
41\end{funcdesc}
42
43\begin{datadesc}{all_errors}
44The set of all exceptions (as a tuple) that methods of \code{FTP}
45instances 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
48\code{socket.error} and \code{IOError}.
49\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
68\subsection{FTP Objects}
69
70FTP instances have the following methods:
71
72\renewcommand{\indexsubitem}{(FTP object method)}
73
74\begin{funcdesc}{set_debuglevel}{level}
75Set the instance's debugging level. This controls the amount of
76debugging output printed. The default, 0, produces no debugging
77output. A value of 1 produces a moderate amount of debugging output,
78generally a single line per request. A value of 2 or higher produces
79the maximum amount of debugging output, logging each line sent and
80received on the control connection.
81\end{funcdesc}
82
83\begin{funcdesc}{connect}{host\optional{\, port}}
84Connect to the given host and port. The default port number is 21, as
85specified 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.
90\end{funcdesc}
91
92\begin{funcdesc}{getwelcome}{}
93Return 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.)
96\end{funcdesc}
97
98\begin{funcdesc}{login}{\optional{user\optional{\, passwd\optional{\, acct}}}}
99Log in as the given \var{user}. The \var{passwd} and \var{acct}
100parameters are optional and default to the empty string. If no
101\var{user} is specified, it defaults to \samp{anonymous}. If
102\var{user} is \code{anonymous}, the default \var{passwd} is
103\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
104name (glanced from the \samp{LOGNAME} or \samp{USER} environment
105variable) and \var{host} is the hostname as returned by
106\code{socket.gethostname()}. This function should be called only
107once 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.
111\end{funcdesc}
112
113\begin{funcdesc}{abort}{}
114Abort a file transfer that is in progress. Using this does not always
115work, but it's worth a try.
116\end{funcdesc}
117
118\begin{funcdesc}{sendcmd}{command}
119Send a simple command string to the server and return the response
120string.
121\end{funcdesc}
122
123\begin{funcdesc}{voidcmd}{command}
124Send 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.
127\end{funcdesc}
128
Guido van Rossumab76af31997-12-03 19:34:14 +0000129\begin{funcdesc}{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
131appropriate \samp{RETR} command, i.e.\ \code{"RETR \var{filename}"}.
132The \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.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000138\end{funcdesc}
139
140\begin{funcdesc}{retrlines}{command\optional{\, callback}}
141Retrieve 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
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000143\code{retrbinary()} or a \samp{LIST} command (usually just the string
144\code{"LIST"}). The \var{callback} function is called for each line,
145with the trailing CRLF stripped. The default \var{callback} prints
146the line to \code{sys.stdout}.
147\end{funcdesc}
148
149\begin{funcdesc}{storbinary}{command\, file\, blocksize}
150Store a file in binary transfer mode. \var{command} should be an
151appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
152\var{file} is an open file object which is read until EOF using its
153\code{read()} method in blocks of size \var{blocksize} to provide the
154data to be stored.
155\end{funcdesc}
156
157\begin{funcdesc}{storlines}{command\, file}
158Store a file in \ASCII{} transfer mode. \var{command} should be an
159appropriate \samp{STOR} command (see \code{storbinary()}). Lines are
160read until EOF from the open file object \var{file} using its
161\code{readline()} method to privide the data to be stored.
162\end{funcdesc}
163
164\begin{funcdesc}{nlst}{argument\optional{\, \ldots}}
165Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000166optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000167server directory). Multiple arguments can be used to pass
168non-standard options to the \samp{NLST} command.
169\end{funcdesc}
170
171\begin{funcdesc}{dir}{argument\optional{\, \ldots}}
172Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000173a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000174(default is the current server directory). Multiple arguments can be
175used to pass non-standard options to the \samp{LIST} command. If the
176last argument is a function, it is used as a \var{callback} function
177as for \code{retrlines()}.
178\end{funcdesc}
179
180\begin{funcdesc}{rename}{fromname\, toname}
181Rename file \var{fromname} on the server to \var{toname}.
182\end{funcdesc}
183
184\begin{funcdesc}{cwd}{pathname}
185Set the current directory on the server.
186\end{funcdesc}
187
188\begin{funcdesc}{mkd}{pathname}
189Create a new directory on the server.
190\end{funcdesc}
191
192\begin{funcdesc}{pwd}{}
193Return the pathname of the current directory on the server.
194\end{funcdesc}
195
196\begin{funcdesc}{quit}{}
197Send a \samp{QUIT} command to the server and close the connection.
198This is the ``polite'' way to close a connection, but it may raise an
199exception of the server reponds with an error to the \code{QUIT}
200command.
201\end{funcdesc}
202
203\begin{funcdesc}{close}{}
204Close the connection unilaterally. This should not be applied to an
205already closed connection (e.g.\ after a successful call to
206\code{quit()}.
207\end{funcdesc}