Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{ftplib} --- |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 2 | FTP protocol client} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{ftplib} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{FTP protocol client (requires sockets).} |
| 6 | |
Fred Drake | ef338ec | 2001-12-26 19:48:43 +0000 | [diff] [blame] | 7 | \indexii{FTP}{protocol} |
| 8 | \index{FTP!\module{ftplib} (standard module)} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 9 | |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 10 | This module defines the class \class{FTP} and a few related items. |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 11 | The \class{FTP} class implements the client side of the FTP |
Fred Drake | ef338ec | 2001-12-26 19:48:43 +0000 | [diff] [blame] | 12 | protocol. You can use this to write Python |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 13 | programs that perform a variety of automated FTP jobs, such as |
| 14 | mirroring other ftp servers. It is also used by the module |
| 15 | \refmodule{urllib} to handle URLs that use FTP. For more information |
| 16 | on FTP (File Transfer Protocol), see Internet \rfc{959}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 17 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 18 | Here's a sample session using the \module{ftplib} module: |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 19 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 20 | \begin{verbatim} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 21 | >>> from ftplib import FTP |
| 22 | >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 23 | >>> ftp.login() # user anonymous, passwd user@hostname |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 24 | >>> ftp.retrlines('LIST') # list directory contents |
| 25 | total 24418 |
| 26 | drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 . |
| 27 | dr-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 | . |
Fred Drake | 161edc2 | 1998-08-07 17:30:49 +0000 | [diff] [blame] | 32 | >>> ftp.retrbinary('RETR README', open('README', 'wb').write) |
| 33 | '226 Transfer complete.' |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 34 | >>> ftp.quit() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 35 | \end{verbatim} |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 37 | The module defines the following items: |
| 38 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 39 | \begin{classdesc}{FTP}{\optional{host\optional{, user\optional{, |
| 40 | passwd\optional{, acct}}}}} |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 41 | Return a new instance of the \class{FTP} class. When |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 42 | \var{host} is given, the method call \code{connect(\var{host})} is |
| 43 | made. 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). |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 46 | \end{classdesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 47 | |
| 48 | \begin{datadesc}{all_errors} |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 49 | The set of all exceptions (as a tuple) that methods of \class{FTP} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 50 | instances may raise as a result of problems with the FTP connection |
| 51 | (as opposed to programming errors made by the caller). This set |
| 52 | includes the four exceptions listed below as well as |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 53 | \exception{socket.error} and \exception{IOError}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 54 | \end{datadesc} |
| 55 | |
| 56 | \begin{excdesc}{error_reply} |
| 57 | Exception raised when an unexpected reply is received from the server. |
| 58 | \end{excdesc} |
| 59 | |
| 60 | \begin{excdesc}{error_temp} |
| 61 | Exception raised when an error code in the range 400--499 is received. |
| 62 | \end{excdesc} |
| 63 | |
| 64 | \begin{excdesc}{error_perm} |
| 65 | Exception raised when an error code in the range 500--599 is received. |
| 66 | \end{excdesc} |
| 67 | |
| 68 | \begin{excdesc}{error_proto} |
| 69 | Exception raised when a reply is received from the server that does |
| 70 | not begin with a digit in the range 1--5. |
| 71 | \end{excdesc} |
| 72 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 73 | |
Fred Drake | e82f5b3 | 1999-03-25 05:04:17 +0000 | [diff] [blame] | 74 | \begin{seealso} |
Fred Drake | 184e836 | 1999-05-11 15:14:15 +0000 | [diff] [blame] | 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.} |
Fred Drake | e82f5b3 | 1999-03-25 05:04:17 +0000 | [diff] [blame] | 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 | |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 86 | \subsection{FTP Objects \label{ftp-objects}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 87 | |
Fred Drake | 33378da | 1999-05-17 16:35:15 +0000 | [diff] [blame] | 88 | Several methods are available in two flavors: one for handling text |
| 89 | files and another for binary files. These are named for the command |
| 90 | which is used followed by \samp{lines} for the text version or |
| 91 | \samp{binary} for the binary version. |
| 92 | |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 93 | \class{FTP} instances have the following methods: |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 94 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 95 | \begin{methoddesc}{set_debuglevel}{level} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 96 | Set the instance's debugging level. This controls the amount of |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 97 | debugging output printed. The default, \code{0}, produces no |
| 98 | debugging output. A value of \code{1} produces a moderate amount of |
| 99 | debugging output, generally a single line per request. A value of |
| 100 | \code{2} or higher produces the maximum amount of debugging output, |
| 101 | logging each line sent and received on the control connection. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 102 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 103 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 104 | \begin{methoddesc}{connect}{host\optional{, port}} |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 105 | Connect to the given host and port. The default port number is \code{21}, as |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 106 | specified by the FTP protocol specification. It is rarely needed to |
| 107 | specify a different port number. This function should be called only |
| 108 | once for each instance; it should not be called at all if a host was |
| 109 | given when the instance was created. All other methods can only be |
| 110 | used after a connection has been made. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 111 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 112 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 113 | \begin{methoddesc}{getwelcome}{} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 114 | Return the welcome message sent by the server in reply to the initial |
| 115 | connection. (This message sometimes contains disclaimers or help |
| 116 | information that may be relevant to the user.) |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 117 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 118 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 119 | \begin{methoddesc}{login}{\optional{user\optional{, passwd\optional{, acct}}}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 120 | Log in as the given \var{user}. The \var{passwd} and \var{acct} |
| 121 | parameters are optional and default to the empty string. If no |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 122 | \var{user} is specified, it defaults to \code{'anonymous'}. If |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 123 | \var{user} is \code{'anonymous'}, the default \var{passwd} is |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 124 | \samp{\var{realuser}@\var{host}} where \var{realuser} is the real user |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 125 | name (glanced from the \envvar{LOGNAME} or \envvar{USER} environment |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 126 | variable) and \var{host} is the hostname as returned by |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 127 | \function{socket.gethostname()}. This function should be called only |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 128 | once for each instance, after a connection has been established; it |
| 129 | should not be called at all if a host and user were given when the |
| 130 | instance was created. Most FTP commands are only allowed after the |
| 131 | client has logged in. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 132 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 133 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 134 | \begin{methoddesc}{abort}{} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 135 | Abort a file transfer that is in progress. Using this does not always |
| 136 | work, but it's worth a try. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 137 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 138 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 139 | \begin{methoddesc}{sendcmd}{command} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 140 | Send a simple command string to the server and return the response |
| 141 | string. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 142 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 143 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 144 | \begin{methoddesc}{voidcmd}{command} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 145 | Send a simple command string to the server and handle the response. |
| 146 | Return nothing if a response code in the range 200--299 is received. |
| 147 | Raise an exception otherwise. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 148 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 149 | |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 150 | \begin{methoddesc}{retrbinary}{command, |
| 151 | callback\optional{, maxblocksize\optional{, rest}}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 152 | Retrieve a file in binary transfer mode. \var{command} should be an |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 153 | appropriate \samp{RETR} command: \code{'RETR \var{filename}'}. |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 154 | The \var{callback} function is called for each block of data received, |
| 155 | with a single string argument giving the data block. |
Guido van Rossum | ab76af3 | 1997-12-03 19:34:14 +0000 | [diff] [blame] | 156 | The optional \var{maxblocksize} argument specifies the maximum chunk size to |
| 157 | read on the low-level socket object created to do the actual transfer |
| 158 | (which will also be the largest size of the data blocks passed to |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 159 | \var{callback}). A reasonable default is chosen. \var{rest} means the |
| 160 | same thing as in the \method{transfercmd()} method. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 161 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 162 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 163 | \begin{methoddesc}{retrlines}{command\optional{, callback}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 164 | Retrieve a file or directory listing in \ASCII{} transfer mode. |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 165 | \var{command} should be an appropriate \samp{RETR} command (see |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 166 | \method{retrbinary()} or a \samp{LIST} command (usually just the string |
| 167 | \code{'LIST'}). The \var{callback} function is called for each line, |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 168 | with the trailing CRLF stripped. The default \var{callback} prints |
| 169 | the line to \code{sys.stdout}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 170 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 171 | |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 172 | \begin{methoddesc}{set_pasv}{boolean} |
| 173 | Enable ``passive'' mode if \var{boolean} is true, other disable |
Guido van Rossum | 97d3b93 | 2001-01-15 16:37:05 +0000 | [diff] [blame] | 174 | passive mode. (In Python 2.0 and before, passive mode was off by |
| 175 | default; in Python 2.1 and later, it is on by default.) |
Fred Drake | 5967667 | 1999-03-18 16:08:54 +0000 | [diff] [blame] | 176 | \end{methoddesc} |
| 177 | |
Guido van Rossum | bf416fa | 2001-02-15 13:53:40 +0000 | [diff] [blame] | 178 | \begin{methoddesc}{storbinary}{command, file\optional{, blocksize}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 179 | Store a file in binary transfer mode. \var{command} should be an |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 180 | appropriate \samp{STOR} command: \code{"STOR \var{filename}"}. |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 181 | \var{file} is an open file object which is read until \EOF{} using its |
| 182 | \method{read()} method in blocks of size \var{blocksize} to provide the |
Guido van Rossum | bf416fa | 2001-02-15 13:53:40 +0000 | [diff] [blame] | 183 | data to be stored. The \var{blocksize} argument defaults to 8192. |
| 184 | \versionchanged[default for \var{blocksize} added]{2.1} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 185 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 186 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 187 | \begin{methoddesc}{storlines}{command, file} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 188 | Store a file in \ASCII{} transfer mode. \var{command} should be an |
Fred Drake | 6a1eefe | 1998-03-12 06:04:53 +0000 | [diff] [blame] | 189 | appropriate \samp{STOR} command (see \method{storbinary()}). Lines are |
| 190 | read until \EOF{} from the open file object \var{file} using its |
Fred Drake | 5d48e45 | 1999-07-20 13:21:42 +0000 | [diff] [blame] | 191 | \method{readline()} method to provide the data to be stored. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 192 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 193 | |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 194 | \begin{methoddesc}{transfercmd}{cmd\optional{, rest}} |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 195 | Initiate a transfer over the data connection. If the transfer is |
Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 196 | active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 197 | by \var{cmd}, and accept the connection. If the server is passive, |
Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 198 | send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 199 | command. Either way, return the socket for the connection. |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 200 | |
| 201 | If optional \var{rest} is given, a \samp{REST} command is |
| 202 | sent to the server, passing \var{rest} as an argument. \var{rest} is |
| 203 | usually a byte offset into the requested file, telling the server to |
| 204 | restart sending the file's bytes at the requested offset, skipping |
| 205 | over the initial bytes. Note however that RFC |
| 206 | 959 requires only that \var{rest} be a string containing characters |
| 207 | in the printable range from ASCII code 33 to ASCII code 126. The |
| 208 | \method{transfercmd()} method, therefore, converts |
| 209 | \var{rest} to a string, but no check is |
| 210 | performed on the string's contents. If the server does |
| 211 | not recognize the \samp{REST} command, an |
| 212 | \exception{error_reply} exception will be raised. If this happens, |
| 213 | simply call \method{transfercmd()} without a \var{rest} argument. |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 214 | \end{methoddesc} |
| 215 | |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 216 | \begin{methoddesc}{ntransfercmd}{cmd\optional{, rest}} |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 217 | Like \method{transfercmd()}, but returns a tuple of the data |
| 218 | connection and the expected size of the data. If the expected size |
| 219 | could not be computed, \code{None} will be returned as the expected |
Barry Warsaw | 21fbd54 | 2000-09-01 06:32:32 +0000 | [diff] [blame] | 220 | size. \var{cmd} and \var{rest} means the same thing as in |
| 221 | \method{transfercmd()}. |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 222 | \end{methoddesc} |
| 223 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 224 | \begin{methoddesc}{nlst}{argument\optional{, \ldots}} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 225 | Return a list of files as returned by the \samp{NLST} command. The |
Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 226 | optional \var{argument} is a directory to list (default is the current |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 227 | server directory). Multiple arguments can be used to pass |
| 228 | non-standard options to the \samp{NLST} command. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 229 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 230 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 231 | \begin{methoddesc}{dir}{argument\optional{, \ldots}} |
Fred Drake | 1508970 | 1999-07-07 13:36:22 +0000 | [diff] [blame] | 232 | Produce a directory listing as returned by the \samp{LIST} command, |
| 233 | printing it to standard output. The optional \var{argument} is a |
| 234 | directory to list (default is the current server directory). Multiple |
| 235 | arguments can be used to pass non-standard options to the \samp{LIST} |
| 236 | command. If the last argument is a function, it is used as a |
| 237 | \var{callback} function as for \method{retrlines()}; the default |
| 238 | prints to \code{sys.stdout}. This method returns \code{None}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 239 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 240 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 241 | \begin{methoddesc}{rename}{fromname, toname} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 242 | Rename file \var{fromname} on the server to \var{toname}. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 243 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 244 | |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 245 | \begin{methoddesc}{delete}{filename} |
| 246 | Remove the file named \var{filename} from the server. If successful, |
| 247 | returns the text of the response, otherwise raises |
Fred Drake | 4429772 | 1999-04-22 16:15:34 +0000 | [diff] [blame] | 248 | \exception{error_perm} on permission errors or |
| 249 | \exception{error_reply} on other errors. |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 250 | \end{methoddesc} |
| 251 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 252 | \begin{methoddesc}{cwd}{pathname} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 253 | Set the current directory on the server. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 254 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 255 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 256 | \begin{methoddesc}{mkd}{pathname} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 257 | Create a new directory on the server. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 258 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 259 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 260 | \begin{methoddesc}{pwd}{} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 261 | Return the pathname of the current directory on the server. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 262 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 263 | |
Fred Drake | 4f31694 | 1998-04-27 14:54:06 +0000 | [diff] [blame] | 264 | \begin{methoddesc}{rmd}{dirname} |
| 265 | Remove the directory named \var{dirname} on the server. |
| 266 | \end{methoddesc} |
| 267 | |
| 268 | \begin{methoddesc}{size}{filename} |
| 269 | Request the size of the file named \var{filename} on the server. On |
| 270 | success, the size of the file is returned as an integer, otherwise |
| 271 | \code{None} is returned. Note that the \samp{SIZE} command is not |
| 272 | standardized, but is supported by many common server implementations. |
| 273 | \end{methoddesc} |
| 274 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 275 | \begin{methoddesc}{quit}{} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 276 | Send a \samp{QUIT} command to the server and close the connection. |
| 277 | This is the ``polite'' way to close a connection, but it may raise an |
Fred Drake | 4429772 | 1999-04-22 16:15:34 +0000 | [diff] [blame] | 278 | exception of the server reponds with an error to the |
| 279 | \samp{QUIT} command. This implies a call to the \method{close()} |
| 280 | method which renders the \class{FTP} instance useless for subsequent |
| 281 | calls (see below). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 282 | \end{methoddesc} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 283 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 284 | \begin{methoddesc}{close}{} |
Guido van Rossum | cca8d2b | 1995-03-22 15:48:46 +0000 | [diff] [blame] | 285 | Close the connection unilaterally. This should not be applied to an |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 286 | already closed connection (such as after a successful call to |
Guido van Rossum | 730d837 | 1998-08-07 17:36:59 +0000 | [diff] [blame] | 287 | \method{quit()}. After this call the \class{FTP} instance should not |
Fred Drake | 91f2f26 | 2001-07-06 19:28:48 +0000 | [diff] [blame] | 288 | be used any more (after a call to \method{close()} or |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 289 | \method{quit()} you cannot reopen the connection by issuing another |
Guido van Rossum | 730d837 | 1998-08-07 17:36:59 +0000 | [diff] [blame] | 290 | \method{login()} method). |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 291 | \end{methoddesc} |