blob: 39f565722db6d3afc5e183c0893a52243bbcab36 [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
Fred Drake19479911998-02-13 06:58:54 +00006\setindexsubitem{(in module ftplib)}
Guido van Rossum86751151995-02-28 17:14:32 +00007
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00008This module defines the class \code{FTP} and a few related items. The
9\code{FTP} class implements the client side of the FTP protocol. You
10can use this to write Python programs that perform a variety of
11automated FTP jobs, such as mirroring other ftp servers. It is also
Guido van Rossum0bbbea11995-08-10 14:21:11 +000012used by the module \code{urllib} to handle URLs that use FTP. For
Fred Drakec5891241998-02-09 19:16:20 +000013more information on FTP (File Transfer Protocol), see Internet \rfc{959}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000014
15Here's a sample session using the \code{ftplib} module:
16
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}
Guido van Rossume47da0a1997-07-17 16:34:52 +000031%
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
Fred Drake19479911998-02-13 06:58:54 +000071\setindexsubitem{(FTP object method)}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000072
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
Guido van Rossumab76af31997-12-03 19:34:14 +0000128\begin{funcdesc}{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
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.
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.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000137\end{funcdesc}
138
139\begin{funcdesc}{retrlines}{command\optional{\, callback}}
140Retrieve 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
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000142\code{retrbinary()} or a \samp{LIST} command (usually just the string
143\code{"LIST"}). The \var{callback} function is called for each line,
144with the trailing CRLF stripped. The default \var{callback} prints
145the line to \code{sys.stdout}.
146\end{funcdesc}
147
148\begin{funcdesc}{storbinary}{command\, file\, blocksize}
149Store a file in binary transfer mode. \var{command} should be an
150appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
151\var{file} is an open file object which is read until EOF using its
152\code{read()} method in blocks of size \var{blocksize} to provide the
153data to be stored.
154\end{funcdesc}
155
156\begin{funcdesc}{storlines}{command\, file}
157Store a file in \ASCII{} transfer mode. \var{command} should be an
158appropriate \samp{STOR} command (see \code{storbinary()}). Lines are
159read until EOF from the open file object \var{file} using its
160\code{readline()} method to privide the data to be stored.
161\end{funcdesc}
162
163\begin{funcdesc}{nlst}{argument\optional{\, \ldots}}
164Return 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.
168\end{funcdesc}
169
170\begin{funcdesc}{dir}{argument\optional{\, \ldots}}
171Return 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
176as for \code{retrlines()}.
177\end{funcdesc}
178
179\begin{funcdesc}{rename}{fromname\, toname}
180Rename file \var{fromname} on the server to \var{toname}.
181\end{funcdesc}
182
183\begin{funcdesc}{cwd}{pathname}
184Set the current directory on the server.
185\end{funcdesc}
186
187\begin{funcdesc}{mkd}{pathname}
188Create a new directory on the server.
189\end{funcdesc}
190
191\begin{funcdesc}{pwd}{}
192Return the pathname of the current directory on the server.
193\end{funcdesc}
194
195\begin{funcdesc}{quit}{}
196Send 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
198exception of the server reponds with an error to the \code{QUIT}
199command.
200\end{funcdesc}
201
202\begin{funcdesc}{close}{}
203Close the connection unilaterally. This should not be applied to an
204already closed connection (e.g.\ after a successful call to
205\code{quit()}.
206\end{funcdesc}