blob: c8e6ed39e6b5bc236cbb7a1b5d26eacb5de0754b [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}
5\index{RFC!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
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000014more information on FTP (File Transfer Protocol), see Internet RFC
15959.
16
17Here's a sample session using the \code{ftplib} module:
18
Guido van Rossume47da0a1997-07-17 16:34:52 +000019\bcode\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000020>>> from ftplib import FTP
21>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
Guido van Rossum96628a91995-04-10 11:34:00 +000022>>> ftp.login() # user anonymous, passwd user@hostname
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000023>>> ftp.retrlines('LIST') # list directory contents
24total 24418
25drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
26dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
27-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
28 .
29 .
30 .
31>>> ftp.quit()
Guido van Rossume47da0a1997-07-17 16:34:52 +000032\end{verbatim}\ecode
33%
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000034The module defines the following items:
35
36\begin{funcdesc}{FTP}{\optional{host\optional{\, user\, passwd\, acct}}}
37Return a new instance of the \code{FTP} class. When
38\var{host} is given, the method call \code{connect(\var{host})} is
39made. When \var{user} is given, additionally the method call
40\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
41\var{passwd} and \var{acct} default to the empty string when not given).
42\end{funcdesc}
43
44\begin{datadesc}{all_errors}
45The set of all exceptions (as a tuple) that methods of \code{FTP}
46instances may raise as a result of problems with the FTP connection
47(as opposed to programming errors made by the caller). This set
48includes the four exceptions listed below as well as
49\code{socket.error} and \code{IOError}.
50\end{datadesc}
51
52\begin{excdesc}{error_reply}
53Exception raised when an unexpected reply is received from the server.
54\end{excdesc}
55
56\begin{excdesc}{error_temp}
57Exception raised when an error code in the range 400--499 is received.
58\end{excdesc}
59
60\begin{excdesc}{error_perm}
61Exception raised when an error code in the range 500--599 is received.
62\end{excdesc}
63
64\begin{excdesc}{error_proto}
65Exception raised when a reply is received from the server that does
66not begin with a digit in the range 1--5.
67\end{excdesc}
68
69\subsection{FTP Objects}
70
71FTP instances have the following methods:
72
73\renewcommand{\indexsubitem}{(FTP object method)}
74
75\begin{funcdesc}{set_debuglevel}{level}
76Set the instance's debugging level. This controls the amount of
77debugging output printed. The default, 0, produces no debugging
78output. A value of 1 produces a moderate amount of debugging output,
79generally a single line per request. A value of 2 or higher produces
80the maximum amount of debugging output, logging each line sent and
81received on the control connection.
82\end{funcdesc}
83
84\begin{funcdesc}{connect}{host\optional{\, port}}
85Connect to the given host and port. The default port number is 21, as
86specified by the FTP protocol specification. It is rarely needed to
87specify a different port number. This function should be called only
88once for each instance; it should not be called at all if a host was
89given when the instance was created. All other methods can only be
90used after a connection has been made.
91\end{funcdesc}
92
93\begin{funcdesc}{getwelcome}{}
94Return the welcome message sent by the server in reply to the initial
95connection. (This message sometimes contains disclaimers or help
96information that may be relevant to the user.)
97\end{funcdesc}
98
99\begin{funcdesc}{login}{\optional{user\optional{\, passwd\optional{\, acct}}}}
100Log in as the given \var{user}. The \var{passwd} and \var{acct}
101parameters are optional and default to the empty string. If no
102\var{user} is specified, it defaults to \samp{anonymous}. If
103\var{user} is \code{anonymous}, the default \var{passwd} is
104\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
105name (glanced from the \samp{LOGNAME} or \samp{USER} environment
106variable) and \var{host} is the hostname as returned by
107\code{socket.gethostname()}. This function should be called only
108once for each instance, after a connection has been established; it
109should not be called at all if a host and user were given when the
110instance was created. Most FTP commands are only allowed after the
111client has logged in.
112\end{funcdesc}
113
114\begin{funcdesc}{abort}{}
115Abort a file transfer that is in progress. Using this does not always
116work, but it's worth a try.
117\end{funcdesc}
118
119\begin{funcdesc}{sendcmd}{command}
120Send a simple command string to the server and return the response
121string.
122\end{funcdesc}
123
124\begin{funcdesc}{voidcmd}{command}
125Send a simple command string to the server and handle the response.
126Return nothing if a response code in the range 200--299 is received.
127Raise an exception otherwise.
128\end{funcdesc}
129
Guido van Rossumab76af31997-12-03 19:34:14 +0000130\begin{funcdesc}{retrbinary}{command\, callback\optional{\, maxblocksize}}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000131Retrieve a file in binary transfer mode. \var{command} should be an
132appropriate \samp{RETR} command, i.e.\ \code{"RETR \var{filename}"}.
133The \var{callback} function is called for each block of data received,
134with a single string argument giving the data block.
Guido van Rossumab76af31997-12-03 19:34:14 +0000135The optional \var{maxblocksize} argument specifies the maximum chunk size to
136read on the low-level socket object created to do the actual transfer
137(which will also be the largest size of the data blocks passed to
138\var{callback}). A reasonable default is chosen.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000139\end{funcdesc}
140
141\begin{funcdesc}{retrlines}{command\optional{\, callback}}
142Retrieve a file or directory listing in \ASCII{} transfer mode.
Fred Drake4b3f0311996-12-13 22:04:31 +0000143\var{command} should be an appropriate \samp{RETR} command (see
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000144\code{retrbinary()} or a \samp{LIST} command (usually just the string
145\code{"LIST"}). The \var{callback} function is called for each line,
146with the trailing CRLF stripped. The default \var{callback} prints
147the line to \code{sys.stdout}.
148\end{funcdesc}
149
150\begin{funcdesc}{storbinary}{command\, file\, blocksize}
151Store a file in binary transfer mode. \var{command} should be an
152appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
153\var{file} is an open file object which is read until EOF using its
154\code{read()} method in blocks of size \var{blocksize} to provide the
155data to be stored.
156\end{funcdesc}
157
158\begin{funcdesc}{storlines}{command\, file}
159Store a file in \ASCII{} transfer mode. \var{command} should be an
160appropriate \samp{STOR} command (see \code{storbinary()}). Lines are
161read until EOF from the open file object \var{file} using its
162\code{readline()} method to privide the data to be stored.
163\end{funcdesc}
164
165\begin{funcdesc}{nlst}{argument\optional{\, \ldots}}
166Return a list of files as returned by the \samp{NLST} command. The
Fred Drake4b3f0311996-12-13 22:04:31 +0000167optional \var{argument} is a directory to list (default is the current
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000168server directory). Multiple arguments can be used to pass
169non-standard options to the \samp{NLST} command.
170\end{funcdesc}
171
172\begin{funcdesc}{dir}{argument\optional{\, \ldots}}
173Return a directory listing as returned by the \samp{LIST} command, as
Fred Drake4b3f0311996-12-13 22:04:31 +0000174a list of lines. The optional \var{argument} is a directory to list
Guido van Rossumcca8d2b1995-03-22 15:48:46 +0000175(default is the current server directory). Multiple arguments can be
176used to pass non-standard options to the \samp{LIST} command. If the
177last argument is a function, it is used as a \var{callback} function
178as for \code{retrlines()}.
179\end{funcdesc}
180
181\begin{funcdesc}{rename}{fromname\, toname}
182Rename file \var{fromname} on the server to \var{toname}.
183\end{funcdesc}
184
185\begin{funcdesc}{cwd}{pathname}
186Set the current directory on the server.
187\end{funcdesc}
188
189\begin{funcdesc}{mkd}{pathname}
190Create a new directory on the server.
191\end{funcdesc}
192
193\begin{funcdesc}{pwd}{}
194Return the pathname of the current directory on the server.
195\end{funcdesc}
196
197\begin{funcdesc}{quit}{}
198Send a \samp{QUIT} command to the server and close the connection.
199This is the ``polite'' way to close a connection, but it may raise an
200exception of the server reponds with an error to the \code{QUIT}
201command.
202\end{funcdesc}
203
204\begin{funcdesc}{close}{}
205Close the connection unilaterally. This should not be applied to an
206already closed connection (e.g.\ after a successful call to
207\code{quit()}.
208\end{funcdesc}