libftplib.tex 12.2 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{ftplib} ---
Fred Drake's avatar
Fred Drake committed
2
         FTP protocol client}
3

Fred Drake's avatar
Fred Drake committed
4
\declaremodule{standard}{ftplib}
5 6
\modulesynopsis{FTP protocol client (requires sockets).}

7 8
\indexii{FTP}{protocol}
\index{FTP!\module{ftplib} (standard module)}
9

10
This module defines the class \class{FTP} and a few related items.
Fred Drake's avatar
Fred Drake committed
11
The \class{FTP} class implements the client side of the FTP
12
protocol.  You can use this to write Python
Fred Drake's avatar
Fred Drake committed
13 14 15 16
programs that perform a variety of automated FTP jobs, such as
mirroring other ftp servers.  It is also used by the module
\refmodule{urllib} to handle URLs that use FTP.  For more information
on FTP (File Transfer Protocol), see Internet \rfc{959}.
Guido van Rossum's avatar
Guido van Rossum committed
17

Fred Drake's avatar
Fred Drake committed
18
Here's a sample session using the \module{ftplib} module:
Guido van Rossum's avatar
Guido van Rossum committed
19

20
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
21 22
>>> from ftplib import FTP
>>> ftp = FTP('ftp.cwi.nl')   # connect to host, default port
23
>>> ftp.login()               # user anonymous, passwd user@hostname
Guido van Rossum's avatar
Guido van Rossum committed
24 25 26 27 28 29 30 31
>>> ftp.retrlines('LIST')     # list directory contents
total 24418
drwxrwsr-x   5 ftp-usr  pdmaint     1536 Mar 20 09:48 .
dr-xr-srwt 105 ftp-usr  pdmaint     1536 Mar 21 14:32 ..
-rw-r--r--   1 ftp-usr  pdmaint     5305 Mar 20 09:48 INDEX
 .
 .
 .
32 33
>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
'226 Transfer complete.'
Guido van Rossum's avatar
Guido van Rossum committed
34
>>> ftp.quit()
35
\end{verbatim}
Fred Drake's avatar
Fred Drake committed
36

Guido van Rossum's avatar
Guido van Rossum committed
37 38
The module defines the following items:

39 40
\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
                       passwd\optional{, acct}}}}}
41
Return a new instance of the \class{FTP} class.  When
Guido van Rossum's avatar
Guido van Rossum committed
42 43 44 45
\var{host} is given, the method call \code{connect(\var{host})} is
made.  When \var{user} is given, additionally the method call
\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
\var{passwd} and \var{acct} default to the empty string when not given).
Fred Drake's avatar
Fred Drake committed
46
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
47 48

\begin{datadesc}{all_errors}
Fred Drake's avatar
Fred Drake committed
49
The set of all exceptions (as a tuple) that methods of \class{FTP}
Guido van Rossum's avatar
Guido van Rossum committed
50 51 52
instances may raise as a result of problems with the FTP connection
(as opposed to programming errors made by the caller).  This set
includes the four exceptions listed below as well as
Fred Drake's avatar
Fred Drake committed
53
\exception{socket.error} and \exception{IOError}.
Guido van Rossum's avatar
Guido van Rossum committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
\end{datadesc}

\begin{excdesc}{error_reply}
Exception raised when an unexpected reply is received from the server.
\end{excdesc}

\begin{excdesc}{error_temp}
Exception raised when an error code in the range 400--499 is received.
\end{excdesc}

\begin{excdesc}{error_perm}
Exception raised when an error code in the range 500--599 is received.
\end{excdesc}

\begin{excdesc}{error_proto}
Exception raised when a reply is received from the server that does
not begin with a digit in the range 1--5.
\end{excdesc}

73

74
\begin{seealso}
75 76 77 78
  \seemodule{netrc}{Parser for the \file{.netrc} file format.  The file
                    \file{.netrc} is typically used by FTP clients to
                    load user authentication information before prompting
                    the user.}
79 80 81 82 83 84 85
  \seetext{The file \file{Tools/scripts/ftpmirror.py}\index{ftpmirror.py}
           in the Python source distribution is a script that can mirror
           FTP sites, or portions thereof, using the \module{ftplib} module.
           It can be used as an extended example that applies this module.}
\end{seealso}


Fred Drake's avatar
Fred Drake committed
86
\subsection{FTP Objects \label{ftp-objects}}
Guido van Rossum's avatar
Guido van Rossum committed
87

88 89 90 91 92
Several methods are available in two flavors: one for handling text
files and another for binary files.  These are named for the command
which is used followed by \samp{lines} for the text version or
\samp{binary} for the binary version.

Fred Drake's avatar
Fred Drake committed
93
\class{FTP} instances have the following methods:
Guido van Rossum's avatar
Guido van Rossum committed
94

95
\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum's avatar
Guido van Rossum committed
96
Set the instance's debugging level.  This controls the amount of
Fred Drake's avatar
Fred Drake committed
97 98 99 100 101
debugging output printed.  The default, \code{0}, produces no
debugging output.  A value of \code{1} produces a moderate amount of
debugging output, generally a single line per request.  A value of
\code{2} or higher produces the maximum amount of debugging output,
logging each line sent and received on the control connection.
102
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
103

104
\begin{methoddesc}{connect}{host\optional{, port}}
Fred Drake's avatar
Fred Drake committed
105
Connect to the given host and port.  The default port number is \code{21}, as
Guido van Rossum's avatar
Guido van Rossum committed
106 107 108 109 110
specified by the FTP protocol specification.  It is rarely needed to
specify a different port number.  This function should be called only
once for each instance; it should not be called at all if a host was
given when the instance was created.  All other methods can only be
used after a connection has been made.
111
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
112

113
\begin{methoddesc}{getwelcome}{}
Guido van Rossum's avatar
Guido van Rossum committed
114 115 116
Return the welcome message sent by the server in reply to the initial
connection.  (This message sometimes contains disclaimers or help
information that may be relevant to the user.)
117
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
118

119
\begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}}
Guido van Rossum's avatar
Guido van Rossum committed
120 121
Log in as the given \var{user}.  The \var{passwd} and \var{acct}
parameters are optional and default to the empty string.  If no
Fred Drake's avatar
Fred Drake committed
122
\var{user} is specified, it defaults to \code{'anonymous'}.  If
Fred Drake's avatar
Fred Drake committed
123
\var{user} is \code{'anonymous'}, the default \var{passwd} is
Guido van Rossum's avatar
Guido van Rossum committed
124
\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
Fred Drake's avatar
Fred Drake committed
125
name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment
Guido van Rossum's avatar
Guido van Rossum committed
126
variable) and \var{host} is the hostname as returned by
Fred Drake's avatar
Fred Drake committed
127
\function{socket.gethostname()}.  This function should be called only
Guido van Rossum's avatar
Guido van Rossum committed
128 129 130 131
once for each instance, after a connection has been established; it
should not be called at all if a host and user were given when the
instance was created.  Most FTP commands are only allowed after the
client has logged in.
132
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
133

134
\begin{methoddesc}{abort}{}
Guido van Rossum's avatar
Guido van Rossum committed
135 136
Abort a file transfer that is in progress.  Using this does not always
work, but it's worth a try.
137
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
138

139
\begin{methoddesc}{sendcmd}{command}
Guido van Rossum's avatar
Guido van Rossum committed
140 141
Send a simple command string to the server and return the response
string.
142
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
143

144
\begin{methoddesc}{voidcmd}{command}
Guido van Rossum's avatar
Guido van Rossum committed
145 146 147
Send a simple command string to the server and handle the response.
Return nothing if a response code in the range 200--299 is received.
Raise an exception otherwise.
148
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
149

150 151
\begin{methoddesc}{retrbinary}{command,
    callback\optional{, maxblocksize\optional{, rest}}}
Guido van Rossum's avatar
Guido van Rossum committed
152
Retrieve a file in binary transfer mode.  \var{command} should be an
153
appropriate \samp{RETR} command: \code{'RETR \var{filename}'}.
Guido van Rossum's avatar
Guido van Rossum committed
154 155
The \var{callback} function is called for each block of data received,
with a single string argument giving the data block.
156 157 158
The optional \var{maxblocksize} argument specifies the maximum chunk size to
read on the low-level socket object created to do the actual transfer
(which will also be the largest size of the data blocks passed to
159 160
\var{callback}).  A reasonable default is chosen. \var{rest} means the
same thing as in the \method{transfercmd()} method.
161
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
162

163
\begin{methoddesc}{retrlines}{command\optional{, callback}}
Guido van Rossum's avatar
Guido van Rossum committed
164
Retrieve a file or directory listing in \ASCII{} transfer mode.
165
\var{command} should be an appropriate \samp{RETR} command (see
Fred Drake's avatar
Fred Drake committed
166 167
\method{retrbinary()} or a \samp{LIST} command (usually just the string
\code{'LIST'}).  The \var{callback} function is called for each line,
Guido van Rossum's avatar
Guido van Rossum committed
168 169
with the trailing CRLF stripped.  The default \var{callback} prints
the line to \code{sys.stdout}.
170
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
171

Fred Drake's avatar
Fred Drake committed
172 173
\begin{methoddesc}{set_pasv}{boolean}
Enable ``passive'' mode if \var{boolean} is true, other disable
174 175
passive mode.  (In Python 2.0 and before, passive mode was off by
default; in Python 2.1 and later, it is on by default.)
Fred Drake's avatar
Fred Drake committed
176 177
\end{methoddesc}

178
\begin{methoddesc}{storbinary}{command, file\optional{, blocksize}}
Guido van Rossum's avatar
Guido van Rossum committed
179
Store a file in binary transfer mode.  \var{command} should be an
180
appropriate \samp{STOR} command: \code{"STOR \var{filename}"}.
Fred Drake's avatar
Fred Drake committed
181 182
\var{file} is an open file object which is read until \EOF{} using its
\method{read()} method in blocks of size \var{blocksize} to provide the
183 184
data to be stored.  The \var{blocksize} argument defaults to 8192.
\versionchanged[default for \var{blocksize} added]{2.1}
185
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
186

187
\begin{methoddesc}{storlines}{command, file}
Guido van Rossum's avatar
Guido van Rossum committed
188
Store a file in \ASCII{} transfer mode.  \var{command} should be an
Fred Drake's avatar
Fred Drake committed
189 190
appropriate \samp{STOR} command (see \method{storbinary()}).  Lines are
read until \EOF{} from the open file object \var{file} using its
Fred Drake's avatar
Fred Drake committed
191
\method{readline()} method to provide the data to be stored.
192
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
193

194
\begin{methoddesc}{transfercmd}{cmd\optional{, rest}}
195
Initiate a transfer over the data connection.  If the transfer is
196
active, send a \samp{EPRT} or  \samp{PORT} command and the transfer command specified
197
by \var{cmd}, and accept the connection.  If the server is passive,
198
send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
199
command.  Either way, return the socket for the connection.
200 201 202 203 204 205 206 207 208 209 210 211 212 213

If optional \var{rest} is given, a \samp{REST} command is
sent to the server, passing \var{rest} as an argument.  \var{rest} is
usually a byte offset into the requested file, telling the server to
restart sending the file's bytes at the requested offset, skipping
over the initial bytes.  Note however that RFC
959 requires only that \var{rest} be a string containing characters
in the printable range from ASCII code 33 to ASCII code 126.  The
\method{transfercmd()} method, therefore, converts
\var{rest} to a string, but no check is
performed on the string's contents.  If the server does
not recognize the \samp{REST} command, an
\exception{error_reply} exception will be raised.  If this happens,
simply call \method{transfercmd()} without a \var{rest} argument.
214 215
\end{methoddesc}

216
\begin{methoddesc}{ntransfercmd}{cmd\optional{, rest}}
217 218 219
Like \method{transfercmd()}, but returns a tuple of the data
connection and the expected size of the data.  If the expected size
could not be computed, \code{None} will be returned as the expected
220 221
size.  \var{cmd} and \var{rest} means the same thing as in
\method{transfercmd()}.
222 223
\end{methoddesc}

224
\begin{methoddesc}{nlst}{argument\optional{, \ldots}}
Guido van Rossum's avatar
Guido van Rossum committed
225
Return a list of files as returned by the \samp{NLST} command.  The
226
optional \var{argument} is a directory to list (default is the current
Guido van Rossum's avatar
Guido van Rossum committed
227 228
server directory).  Multiple arguments can be used to pass
non-standard options to the \samp{NLST} command.
229
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
230

231
\begin{methoddesc}{dir}{argument\optional{, \ldots}}
232 233 234 235 236 237 238
Produce a directory listing as returned by the \samp{LIST} command,
printing it to standard output.  The optional \var{argument} is a
directory to list (default is the current server directory).  Multiple
arguments can be used to pass non-standard options to the \samp{LIST}
command.  If the last argument is a function, it is used as a
\var{callback} function as for \method{retrlines()}; the default
prints to \code{sys.stdout}.  This method returns \code{None}.
239
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
240

241
\begin{methoddesc}{rename}{fromname, toname}
Guido van Rossum's avatar
Guido van Rossum committed
242
Rename file \var{fromname} on the server to \var{toname}.
243
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
244

245 246 247
\begin{methoddesc}{delete}{filename}
Remove the file named \var{filename} from the server.  If successful,
returns the text of the response, otherwise raises
248 249
\exception{error_perm} on permission errors or
\exception{error_reply} on other errors.
250 251
\end{methoddesc}

252
\begin{methoddesc}{cwd}{pathname}
Guido van Rossum's avatar
Guido van Rossum committed
253
Set the current directory on the server.
254
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
255

256
\begin{methoddesc}{mkd}{pathname}
Guido van Rossum's avatar
Guido van Rossum committed
257
Create a new directory on the server.
258
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
259

260
\begin{methoddesc}{pwd}{}
Guido van Rossum's avatar
Guido van Rossum committed
261
Return the pathname of the current directory on the server.
262
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
263

264 265 266 267 268 269 270 271 272 273 274
\begin{methoddesc}{rmd}{dirname}
Remove the directory named \var{dirname} on the server.
\end{methoddesc}

\begin{methoddesc}{size}{filename}
Request the size of the file named \var{filename} on the server.  On
success, the size of the file is returned as an integer, otherwise
\code{None} is returned.  Note that the \samp{SIZE} command is not 
standardized, but is supported by many common server implementations.
\end{methoddesc}

275
\begin{methoddesc}{quit}{}
Guido van Rossum's avatar
Guido van Rossum committed
276 277
Send a \samp{QUIT} command to the server and close the connection.
This is the ``polite'' way to close a connection, but it may raise an
278 279 280 281
exception of the server reponds with an error to the
\samp{QUIT} command.  This implies a call to the \method{close()}
method which renders the \class{FTP} instance useless for subsequent
calls (see below).
282
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
283

284
\begin{methoddesc}{close}{}
Guido van Rossum's avatar
Guido van Rossum committed
285
Close the connection unilaterally.  This should not be applied to an
286
already closed connection (such as after a successful call to
287
\method{quit()}.  After this call the \class{FTP} instance should not
288
be used any more (after a call to \method{close()} or
289
\method{quit()} you cannot reopen the connection by issuing another
290
\method{login()} method).
291
\end{methoddesc}