source: trunk/essentials/dev-lang/python/Doc/lib/libftplib.tex

Last change on this file was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 12.0 KB
Line 
1\section{\module{ftplib} ---
2 FTP protocol client}
3
4\declaremodule{standard}{ftplib}
5\modulesynopsis{FTP protocol client (requires sockets).}
6
7\indexii{FTP}{protocol}
8\index{FTP!\module{ftplib} (standard module)}
9
10This module defines the class \class{FTP} and a few related items.
11The \class{FTP} class implements the client side of the FTP
12protocol. You can use this to write Python
13programs that perform a variety of automated FTP jobs, such as
14mirroring other ftp servers. It is also used by the module
15\refmodule{urllib} to handle URLs that use FTP. For more information
16on FTP (File Transfer Protocol), see Internet \rfc{959}.
17
18Here's a sample session using the \module{ftplib} module:
19
20\begin{verbatim}
21>>> from ftplib import FTP
22>>> ftp = FTP('ftp.cwi.nl') # connect to host, default port
23>>> ftp.login() # user anonymous, passwd anonymous@
24>>> ftp.retrlines('LIST') # list directory contents
25total 24418
26drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .
27dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 ..
28-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
29 .
30 .
31 .
32>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
33'226 Transfer complete.'
34>>> ftp.quit()
35\end{verbatim}
36
37The module defines the following items:
38
39\begin{classdesc}{FTP}{\optional{host\optional{, user\optional{,
40 passwd\optional{, acct}}}}}
41Return a new instance of the \class{FTP} class. When
42\var{host} is given, the method call \code{connect(\var{host})} is
43made. When \var{user} is given, additionally the method call
44\code{login(\var{user}, \var{passwd}, \var{acct})} is made (where
45\var{passwd} and \var{acct} default to the empty string when not given).
46\end{classdesc}
47
48\begin{datadesc}{all_errors}
49The set of all exceptions (as a tuple) that methods of \class{FTP}
50instances may raise as a result of problems with the FTP connection
51(as opposed to programming errors made by the caller). This set
52includes the four exceptions listed below as well as
53\exception{socket.error} and \exception{IOError}.
54\end{datadesc}
55
56\begin{excdesc}{error_reply}
57Exception raised when an unexpected reply is received from the server.
58\end{excdesc}
59
60\begin{excdesc}{error_temp}
61Exception raised when an error code in the range 400--499 is received.
62\end{excdesc}
63
64\begin{excdesc}{error_perm}
65Exception raised when an error code in the range 500--599 is received.
66\end{excdesc}
67
68\begin{excdesc}{error_proto}
69Exception raised when a reply is received from the server that does
70not begin with a digit in the range 1--5.
71\end{excdesc}
72
73
74\begin{seealso}
75 \seemodule{netrc}{Parser for the \file{.netrc} file format. The file
76 \file{.netrc} is typically used by FTP clients to
77 load user authentication information before prompting
78 the user.}
79 \seetext{The file \file{Tools/scripts/ftpmirror.py}\index{ftpmirror.py}
80 in the Python source distribution is a script that can mirror
81 FTP sites, or portions thereof, using the \module{ftplib} module.
82 It can be used as an extended example that applies this module.}
83\end{seealso}
84
85
86\subsection{FTP Objects \label{ftp-objects}}
87
88Several methods are available in two flavors: one for handling text
89files and another for binary files. These are named for the command
90which is used followed by \samp{lines} for the text version or
91\samp{binary} for the binary version.
92
93\class{FTP} instances have the following methods:
94
95\begin{methoddesc}{set_debuglevel}{level}
96Set the instance's debugging level. This controls the amount of
97debugging output printed. The default, \code{0}, produces no
98debugging output. A value of \code{1} produces a moderate amount of
99debugging output, generally a single line per request. A value of
100\code{2} or higher produces the maximum amount of debugging output,
101logging each line sent and received on the control connection.
102\end{methoddesc}
103
104\begin{methoddesc}{connect}{host\optional{, port}}
105Connect to the given host and port. The default port number is \code{21}, as
106specified by the FTP protocol specification. It is rarely needed to
107specify a different port number. This function should be called only
108once for each instance; it should not be called at all if a host was
109given when the instance was created. All other methods can only be
110used after a connection has been made.
111\end{methoddesc}
112
113\begin{methoddesc}{getwelcome}{}