summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libftplib.tex
blob: dfbaa2be071d5a76d4c1a6953c41b75b426a0f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
\section{Standard Module \sectcode{ftplib}}
\label{module-ftplib}
\stmodindex{ftplib}

\renewcommand{\indexsubitem}{(in module ftplib)}

This module defines the class \code{FTP} and a few related items.  The
\code{FTP} class implements the client side of the FTP protocol.  You
can use this to write Python programs that perform a variety of
automated FTP jobs, such as mirroring other ftp servers.  It is also
used by the module \code{urllib} to handle URLs that use FTP.  For
more information on FTP (File Transfer Protocol), see Internet RFC
959.

Here's a sample session using the \code{ftplib} module:

\bcode\begin{verbatim}
>>> from ftplib import FTP
>>> ftp = FTP('ftp.cwi.nl')   # connect to host, default port
>>> ftp.login()               # user anonymous, passwd user@hostname
>>> 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
 .
 .
 .
>>> ftp.quit()
\end{verbatim}\ecode
%
The module defines the following items:

\begin{funcdesc}{FTP}{\optional{host\optional{\, user\, passwd\, acct}}}
Return a new instance of the \code{FTP} class.  When
\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).
\end{funcdesc}

\begin{datadesc}{all_errors}
The set of all exceptions (as a tuple) that methods of \code{FTP}
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
\code{socket.error} and \code{IOError}.
\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}

\subsection{FTP Objects}

FTP instances have the following methods:

\renewcommand{\indexsubitem}{(FTP object method)}

\begin{funcdesc}{set_debuglevel}{level}
Set the instance's debugging level.  This controls the amount of
debugging output printed.  The default, 0, produces no debugging
output.  A value of 1 produces a moderate amount of debugging output,
generally a single line per request.  A value of 2 or higher produces
the maximum amount of debugging output, logging each line sent and
received on the control connection.
\end{funcdesc}

\begin{funcdesc}{connect}{host\optional{\, port}}
Connect to the given host and port.  The default port number is 21, as
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.
\end{funcdesc}

\begin{funcdesc}{getwelcome}{}
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.)
\end{funcdesc}

\begin{funcdesc}{login}{\optional{user\optional{\, passwd\optional{\, acct}}}}
Log in as the given \var{user}.  The \var{passwd} and \var{acct}
parameters are optional and default to the empty string.  If no
\var{user} is specified, it defaults to \samp{anonymous}.  If
\var{user} is \code{anonymous}, the default \var{passwd} is
\samp{\var{realuser}@\var{host}} where \var{realuser} is the real user
name (glanced from the \samp{LOGNAME} or \samp{USER} environment
variable) and \var{host} is the hostname as returned by
\code{socket.gethostname()}.  This function should be called only
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.
\end{funcdesc}

\begin{funcdesc}{abort}{}
Abort a file transfer that is in progress.  Using this does not always
work, but it's worth a try.
\end{funcdesc}

\begin{funcdesc}{sendcmd}{command}
Send a simple command string to the server and return the response
string.
\end{funcdesc}

\begin{funcdesc}{voidcmd}{command}
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.
\end{funcdesc}

\begin{funcdesc}{retrbinary}{command\, callback\, maxblocksize}
Retrieve a file in binary transfer mode.  \var{command} should be an
appropriate \samp{RETR} command, i.e.\ \code{"RETR \var{filename}"}.
The \var{callback} function is called for each block of data received,
with a single string argument giving the data block.
The \var{maxblocksize} argument specifies the maximum block size
(which may not be the actual size of the data blocks passed to
\var{callback}).
\end{funcdesc}

\begin{funcdesc}{retrlines}{command\optional{\, callback}}
Retrieve a file or directory listing in \ASCII{} transfer mode.
\var{command} should be an appropriate \samp{RETR} command (see
\code{retrbinary()} or a \samp{LIST} command (usually just the string
\code{"LIST"}).  The \var{callback} function is called for each line,
with the trailing CRLF stripped.  The default \var{callback} prints
the line to \code{sys.stdout}.
\end{funcdesc}

\begin{funcdesc}{storbinary}{command\, file\, blocksize}
Store a file in binary transfer mode.  \var{command} should be an
appropriate \samp{STOR} command, i.e.\ \code{"STOR \var{filename}"}.
\var{file} is an open file object which is read until EOF using its
\code{read()} method in blocks of size \var{blocksize} to provide the
data to be stored.
\end{funcdesc}

\begin{funcdesc}{storlines}{command\, file}
Store a file in \ASCII{} transfer mode.  \var{command} should be an
appropriate \samp{STOR} command (see \code{storbinary()}).  Lines are
read until EOF from the open file object \var{file} using its
\code{readline()} method to privide the data to be stored.
\end{funcdesc}

\begin{funcdesc}{nlst}{argument\optional{\, \ldots}}
Return a list of files as returned by the \samp{NLST} command.  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{NLST} command.
\end{funcdesc}

\begin{funcdesc}{dir}{argument\optional{\, \ldots}}
Return a directory listing as returned by the \samp{LIST} command, as
a list of lines.  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 \code{retrlines()}.
\end{funcdesc}

\begin{funcdesc}{rename}{fromname\, toname}
Rename file \var{fromname} on the server to \var{toname}.
\end{funcdesc}

\begin{funcdesc}{cwd}{pathname}
Set the current directory on the server.
\end{funcdesc}

\begin{funcdesc}{mkd}{pathname}
Create a new directory on the server.
\end{funcdesc}

\begin{funcdesc}{pwd}{}
Return the pathname of the current directory on the server.
\end{funcdesc}

\begin{funcdesc}{quit}{}
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
exception of the server reponds with an error to the \code{QUIT}
command.
\end{funcdesc}

\begin{funcdesc}{close}{}
Close the connection unilaterally.  This should not be applied to an
already closed connection (e.g.\ after a successful call to
\code{quit()}.
\end{funcdesc}