<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>11.7.1 FTP Objects </title> <META NAME="description" CONTENT="11.7.1 FTP Objects "> <META NAME="keywords" CONTENT="lib"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="lib.css"> <link rel="first" href="lib.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="previous" href="module-ftplib.html"> <LINK REL="up" href="module-ftplib.html"> <LINK REL="next" href="module-gopherlib.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="module-ftplib.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="module-ftplib.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-gopherlib.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="module-ftplib.html">11.7 ftplib </A> <b class="navlabel">Up:</b> <a class="sectref" href="module-ftplib.html">11.7 ftplib </A> <b class="navlabel">Next:</b> <a class="sectref" href="module-gopherlib.html">11.8 gopherlib </A> <br><hr> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION0013710000000000000000"> </A> <BR> 11.7.1 FTP Objects </H2> <P> 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 "<tt class="samp">lines</tt>" for the text version or "<tt class="samp">binary</tt>" for the binary version. <P> <tt class="class">FTP</tt> instances have the following methods: <P> <dl><dt><b><a name="l2h-2447"><tt class="method">set_debuglevel</tt></a></b>(<var>level</var>) <dd> Set the instance's debugging level. This controls the amount of debugging output printed. The default, <code>0</code>, produces no debugging output. A value of <code>1</code> produces a moderate amount of debugging output, generally a single line per request. A value of <code>2</code> or higher produces the maximum amount of debugging output, logging each line sent and received on the control connection. </dl> <P> <dl><dt><b><a name="l2h-2448"><tt class="method">connect</tt></a></b>(<var>host</var><big>[</big><var>, port</var><big>]</big>) <dd> Connect to the given host and port. The default port number is <code>21</code>, 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. </dl> <P> <dl><dt><b><a name="l2h-2449"><tt class="method">getwelcome</tt></a></b>() <dd> 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.) </dl> <P> <dl><dt><b><a name="l2h-2450"><tt class="method">login</tt></a></b>(<big>[</big><var>user</var><big>[</big><var>, passwd</var><big>[</big><var>, acct</var><big>]</big><big>]</big><big>]</big>) <dd> Log in as the given <var>user</var>. The <var>passwd</var> and <var>acct</var> parameters are optional and default to the empty string. If no <var>user</var> is specified, it defaults to <code>'anonymous'</code>. If <var>user</var> is <code>'anonymous'</code>, the default <var>passwd</var> is "<tt class="samp"><var>realuser</var>@<var>host</var></tt>" where <var>realuser</var> is the real user name (glanced from the <a class="envvar" name="l2h-2472">LOGNAME</a> or <a class="envvar" name="l2h-2473">USER</a> environment variable) and <var>host</var> is the hostname as returned by <tt class="function">socket.gethostname()</tt>. 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. </dl> <P> <dl><dt><b><a name="l2h-2451"><tt class="method">abort</tt></a></b>() <dd> Abort a file transfer that is in progress. Using this does not always work, but it's worth a try. </dl> <P> <dl><dt><b><a name="l2h-2452"><tt class="method">sendcmd</tt></a></b>(<var>command</var>) <dd> Send a simple command string to the server and return the response string. </dl> <P> <dl><dt><b><a name="l2h-2453"><tt class="method">voidcmd</tt></a></b>(<var>command</var>) <dd> 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. </dl> <P> <dl><dt><b><a name="l2h-2454"><tt class="method">retrbinary</tt></a></b>(<var>command, callback</var><big>[</big><var>, maxblocksize</var><big>[</big><var>, rest</var><big>]</big><big>]</big>) <dd> Retrieve a file in binary transfer mode. <var>command</var> should be an appropriate "<tt class="samp">RETR</tt>" command: <code>'RETR <var>filename</var>'</code>. The <var>callback</var> function is called for each block of data received, with a single string argument giving the data block. The optional <var>maxblocksize</var> 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 <var>callback</var>). A reasonable default is chosen. <var>rest</var> means the same thing as in the <tt class="method">transfercmd()</tt> method. </dl> <P> <dl><dt><b><a name="l2h-2455"><tt class="method">retrlines</tt></a></b>(<var>command</var><big>[</big><var>, callback</var><big>]</big>) <dd> Retrieve a file or directory listing in ASCII transfer mode. <var>command</var> should be an appropriate "<tt class="samp">RETR</tt>" command (see <tt class="method">retrbinary()</tt> or a "<tt class="samp">LIST</tt>" command (usually just the string <code>'LIST'</code>). The <var>callback</var> function is called for each line, with the trailing CRLF stripped. The default <var>callback</var> prints the line to <code>sys.stdout</code>. </dl> <P> <dl><dt><b><a name="l2h-2456"><tt class="method">set_pasv</tt></a></b>(<var>boolean</var>) <dd> Enable ``passive'' mode if <var>boolean</var> is true, other disable 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.) </dl> <P> <dl><dt><b><a name="l2h-2457"><tt class="method">storbinary</tt></a></b>(<var>command, file</var><big>[</big><var>, blocksize</var><big>]</big>) <dd> Store a file in binary transfer mode. <var>command</var> should be an appropriate "<tt class="samp">STOR</tt>" command: <code>"STOR <var>filename</var>"</code>. <var>file</var> is an open file object which is read until EOF using its <tt class="method">read()</tt> method in blocks of size <var>blocksize</var> to provide the data to be stored. The <var>blocksize</var> argument defaults to 8192. <span class="versionnote">Changed in version 2.1: default for <var>blocksize</var> added.</span> </dl> <P> <dl><dt><b><a name="l2h-2458"><tt class="method">storlines</tt></a></b>(<var>command, file</var>) <dd> Store a file in ASCII transfer mode. <var>command</var> should be an appropriate "<tt class="samp">STOR</tt>" command (see <tt class="method">storbinary()</tt>). Lines are read until EOF from the open file object <var>file</var> using its <tt class="method">readline()</tt> method to provide the data to be stored. </dl> <P> <dl><dt><b><a name="l2h-2459"><tt class="method">transfercmd</tt></a></b>(<var>cmd</var><big>[</big><var>, rest</var><big>]</big>) <dd> Initiate a transfer over the data connection. If the transfer is active, send a "<tt class="samp">EPRT</tt>" or "<tt class="samp">PORT</tt>" command and the transfer command specified by <var>cmd</var>, and accept the connection. If the server is passive, send a "<tt class="samp">EPSV</tt>" or "<tt class="samp">PASV</tt>" command, connect to it, and start the transfer command. Either way, return the socket for the connection. <P> If optional <var>rest</var> is given, a "<tt class="samp">REST</tt>" command is sent to the server, passing <var>rest</var> as an argument. <var>rest</var> 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</var> be a string containing characters in the printable range from ASCII code 33 to ASCII code 126. The <tt class="method">transfercmd()</tt> method, therefore, converts <var>rest</var> to a string, but no check is performed on the string's contents. If the server does not recognize the "<tt class="samp">REST</tt>" command, an <tt class="exception">error_reply</tt> exception will be raised. If this happens, simply call <tt class="method">transfercmd()</tt> without a <var>rest</var> argument. </dl> <P> <dl><dt><b><a name="l2h-2460"><tt class="method">ntransfercmd</tt></a></b>(<var>cmd</var><big>[</big><var>, rest</var><big>]</big>) <dd> Like <tt class="method">transfercmd()</tt>, 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</code> will be returned as the expected size. <var>cmd</var> and <var>rest</var> means the same thing as in <tt class="method">transfercmd()</tt>. </dl> <P> <dl><dt><b><a name="l2h-2461"><tt class="method">nlst</tt></a></b>(<var>argument</var><big>[</big><var>, ...</var><big>]</big>) <dd> Return a list of files as returned by the "<tt class="samp">NLST</tt>" command. The optional <var>argument</var> is a directory to list (default is the current server directory). Multiple arguments can be used to pass non-standard options to the "<tt class="samp">NLST</tt>" command. </dl> <P> <dl><dt><b><a name="l2h-2462"><tt class="method">dir</tt></a></b>(<var>argument</var><big>[</big><var>, ...</var><big>]</big>) <dd> Produce a directory listing as returned by the "<tt class="samp">LIST</tt>" command, printing it to standard output. The optional <var>argument</var> is a directory to list (default is the current server directory). Multiple arguments can be used to pass non-standard options to the "<tt class="samp">LIST</tt>"command. If the last argument is a function, it is used as a <var>callback</var> function as for <tt class="method">retrlines()</tt>; the default prints to <code>sys.stdout</code>. This method returns <code>None</code>. </dl> <P> <dl><dt><b><a name="l2h-2463"><tt class="method">rename</tt></a></b>(<var>fromname, toname</var>) <dd> Rename file <var>fromname</var> on the server to <var>toname</var>. </dl> <P> <dl><dt><b><a name="l2h-2464"><tt class="method">delete</tt></a></b>(<var>filename</var>) <dd> Remove the file named <var>filename</var> from the server. If successful, returns the text of the response, otherwise raises <tt class="exception">error_perm</tt> on permission errors or <tt class="exception">error_reply</tt> on other errors. </dl> <P> <dl><dt><b><a name="l2h-2465"><tt class="method">cwd</tt></a></b>(<var>pathname</var>) <dd> Set the current directory on the server. </dl> <P> <dl><dt><b><a name="l2h-2466"><tt class="method">mkd</tt></a></b>(<var>pathname</var>) <dd> Create a new directory on the server. </dl> <P> <dl><dt><b><a name="l2h-2467"><tt class="method">pwd</tt></a></b>() <dd> Return the pathname of the current directory on the server. </dl> <P> <dl><dt><b><a name="l2h-2468"><tt class="method">rmd</tt></a></b>(<var>dirname</var>) <dd> Remove the directory named <var>dirname</var> on the server. </dl> <P> <dl><dt><b><a name="l2h-2469"><tt class="method">size</tt></a></b>(<var>filename</var>) <dd> Request the size of the file named <var>filename</var> on the server. On success, the size of the file is returned as an integer, otherwise <code>None</code> is returned. Note that the "<tt class="samp">SIZE</tt>" command is not standardized, but is supported by many common server implementations. </dl> <P> <dl><dt><b><a name="l2h-2470"><tt class="method">quit</tt></a></b>() <dd> Send a "<tt class="samp">QUIT</tt>" 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 "<tt class="samp">QUIT</tt>" command. This implies a call to the <tt class="method">close()</tt> method which renders the <tt class="class">FTP</tt> instance useless for subsequent calls (see below). </dl> <P> <dl><dt><b><a name="l2h-2471"><tt class="method">close</tt></a></b>() <dd> Close the connection unilaterally. This should not be applied to an already closed connection (such as after a successful call to <tt class="method">quit()</tt>. After this call the <tt class="class">FTP</tt> instance should not be used any more (after a call to <tt class="method">close()</tt> or <tt class="method">quit()</tt> you cannot reopen the connection by issuing another <tt class="method">login()</tt> method). </dl> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="module-ftplib.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="module-ftplib.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-gopherlib.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="module-ftplib.html">11.7 ftplib </A> <b class="navlabel">Up:</b> <a class="sectref" href="module-ftplib.html">11.7 ftplib </A> <b class="navlabel">Next:</b> <a class="sectref" href="module-gopherlib.html">11.8 gopherlib </A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>