1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>20.8. ftplib — FTP protocol client — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="20. Internet Protocols and Support" href="internet.html" />
34
<link rel="next" title="20.9. poplib — POP3 protocol client" href="poplib.html" />
35
<link rel="prev" title="20.7. httplib — HTTP protocol client" href="httplib.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="poplib.html" title="20.9. poplib — POP3 protocol client"
55
accesskey="N">next</a> |</li>
57
<a href="httplib.html" title="20.7. httplib — HTTP protocol client"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-ftplib">
78
<span id="ftplib-ftp-protocol-client"></span><h1>20.8. <a class="reference internal" href="#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">ftplib</span></code></a> — FTP protocol client<a class="headerlink" href="#module-ftplib" title="Permalink to this headline">¶</a></h1>
79
<p id="index-0"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/ftplib.py">Lib/ftplib.py</a></p>
80
<hr class="docutils" />
81
<p>This module defines the class <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> and a few related items. The
82
<a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> class implements the client side of the FTP protocol. You can use
83
this to write Python programs that perform a variety of automated FTP jobs, such
84
as mirroring other ftp servers. It is also used by the module <a class="reference internal" href="urllib.html#module-urllib" title="urllib: Open an arbitrary network resource by URL (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">urllib</span></code></a> to
85
handle URLs that use FTP. For more information on FTP (File Transfer Protocol),
86
see Internet <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc959.html"><strong>RFC 959</strong></a>.</p>
87
<p>Here’s a sample session using the <a class="reference internal" href="#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">ftplib</span></code></a> module:</p>
88
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP</span>
89
<span class="gp">>>> </span><span class="n">ftp</span> <span class="o">=</span> <span class="n">FTP</span><span class="p">(</span><span class="s1">'ftp.debian.org'</span><span class="p">)</span> <span class="c1"># connect to host, default port</span>
90
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">login</span><span class="p">()</span> <span class="c1"># user anonymous, passwd anonymous@</span>
91
<span class="go">'230 Login successful.'</span>
92
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">cwd</span><span class="p">(</span><span class="s1">'debian'</span><span class="p">)</span> <span class="c1"># change into "debian" directory</span>
93
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">retrlines</span><span class="p">(</span><span class="s1">'LIST'</span><span class="p">)</span> <span class="c1"># list directory contents</span>
94
<span class="go">-rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README</span>
95
<span class="gp">...</span>
96
<span class="go">drwxr-sr-x 5 1176 1176 4096 Dec 19 2000 pool</span>
97
<span class="go">drwxr-sr-x 4 1176 1176 4096 Nov 17 2008 project</span>
98
<span class="go">drwxr-xr-x 3 1176 1176 4096 Oct 10 2012 tools</span>
99
<span class="go">'226 Directory send OK.'</span>
100
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">retrbinary</span><span class="p">(</span><span class="s1">'RETR README'</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'README'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">)</span>
101
<span class="go">'226 Transfer complete.'</span>
102
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
105
<p>The module defines the following items:</p>
108
<em class="property">class </em><code class="descclassname">ftplib.</code><code class="descname">FTP</code><span class="sig-paren">(</span><span class="optional">[</span><em>host</em><span class="optional">[</span>, <em>user</em><span class="optional">[</span>, <em>passwd</em><span class="optional">[</span>, <em>acct</em><span class="optional">[</span>, <em>timeout</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP" title="Permalink to this definition">¶</a></dt>
109
<dd><p>Return a new instance of the <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> class. When <em>host</em> is given, the
110
method call <code class="docutils literal"><span class="pre">connect(host)</span></code> is made. When <em>user</em> is given, additionally
111
the method call <code class="docutils literal"><span class="pre">login(user,</span> <span class="pre">passwd,</span> <span class="pre">acct)</span></code> is made (where <em>passwd</em> and
112
<em>acct</em> default to the empty string when not given). The optional <em>timeout</em>
113
parameter specifies a timeout in seconds for blocking operations like the
114
connection attempt (if is not specified, the global default timeout setting
116
<div class="versionchanged">
117
<p><span class="versionmodified">Changed in version 2.6: </span><em>timeout</em> was added.</p>
122
<dt id="ftplib.FTP_TLS">
123
<em class="property">class </em><code class="descclassname">ftplib.</code><code class="descname">FTP_TLS</code><span class="sig-paren">(</span><span class="optional">[</span><em>host</em><span class="optional">[</span>, <em>user</em><span class="optional">[</span>, <em>passwd</em><span class="optional">[</span>, <em>acct</em><span class="optional">[</span>, <em>keyfile</em><span class="optional">[</span>, <em>certfile</em><span class="optional">[</span>, <em>context</em><span class="optional">[</span>, <em>timeout</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS" title="Permalink to this definition">¶</a></dt>
124
<dd><p>A <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> subclass which adds TLS support to FTP as described in
125
<span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4217.html"><strong>RFC 4217</strong></a>.
126
Connect as usual to port 21 implicitly securing the FTP control connection
127
before authenticating. Securing the data connection requires the user to
128
explicitly ask for it by calling the <a class="reference internal" href="#ftplib.FTP_TLS.prot_p" title="ftplib.FTP_TLS.prot_p"><code class="xref py py-meth docutils literal"><span class="pre">prot_p()</span></code></a> method. <em>context</em>
129
is a <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">ssl.SSLContext</span></code></a> object which allows bundling SSL configuration
130
options, certificates and private keys into a single (potentially
131
long-lived) structure. Please read <a class="reference internal" href="ssl.html#ssl-security"><span>Security considerations</span></a> for best practices.</p>
132
<p><em>keyfile</em> and <em>certfile</em> are a legacy alternative to <em>context</em> – they
133
can point to PEM-formatted private key and certificate chain files
134
(respectively) for the SSL connection.</p>
135
<div class="versionadded">
136
<p><span class="versionmodified">New in version 2.7.</span></p>
138
<div class="versionchanged">
139
<p><span class="versionmodified">Changed in version 2.7.10: </span>The <em>context</em> parameter was added.</p>
141
<p>Here’s a sample session using the <a class="reference internal" href="#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal"><span class="pre">FTP_TLS</span></code></a> class:</p>
142
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP_TLS</span>
143
<span class="gp">>>> </span><span class="n">ftps</span> <span class="o">=</span> <span class="n">FTP_TLS</span><span class="p">(</span><span class="s1">'ftp.python.org'</span><span class="p">)</span>
144
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">login</span><span class="p">()</span> <span class="c1"># login anonymously before securing control channel</span>
145
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">prot_p</span><span class="p">()</span> <span class="c1"># switch to secure data connection</span>
146
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">retrlines</span><span class="p">(</span><span class="s1">'LIST'</span><span class="p">)</span> <span class="c1"># list directory content securely</span>
147
<span class="go">total 9</span>
148
<span class="go">drwxr-xr-x 8 root wheel 1024 Jan 3 1994 .</span>
149
<span class="go">drwxr-xr-x 8 root wheel 1024 Jan 3 1994 ..</span>
150
<span class="go">drwxr-xr-x 2 root wheel 1024 Jan 3 1994 bin</span>
151
<span class="go">drwxr-xr-x 2 root wheel 1024 Jan 3 1994 etc</span>
152
<span class="go">d-wxrwxr-x 2 ftp wheel 1024 Sep 5 13:43 incoming</span>
153
<span class="go">drwxr-xr-x 2 root wheel 1024 Nov 17 1993 lib</span>
154
<span class="go">drwxr-xr-x 6 1094 wheel 1024 Sep 13 19:07 pub</span>
155
<span class="go">drwxr-xr-x 3 root wheel 1024 Jan 3 1994 usr</span>
156
<span class="go">-rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg</span>
157
<span class="go">'226 Transfer complete.'</span>
158
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
159
<span class="go">>>></span>
164
<dl class="exception">
165
<dt id="ftplib.error_reply">
166
<em class="property">exception </em><code class="descclassname">ftplib.</code><code class="descname">error_reply</code><a class="headerlink" href="#ftplib.error_reply" title="Permalink to this definition">¶</a></dt>
167
<dd><p>Exception raised when an unexpected reply is received from the server.</p>
170
<dl class="exception">
171
<dt id="ftplib.error_temp">
172
<em class="property">exception </em><code class="descclassname">ftplib.</code><code class="descname">error_temp</code><a class="headerlink" href="#ftplib.error_temp" title="Permalink to this definition">¶</a></dt>
173
<dd><p>Exception raised when an error code signifying a temporary error (response
174
codes in the range 400–499) is received.</p>
177
<dl class="exception">
178
<dt id="ftplib.error_perm">
179
<em class="property">exception </em><code class="descclassname">ftplib.</code><code class="descname">error_perm</code><a class="headerlink" href="#ftplib.error_perm" title="Permalink to this definition">¶</a></dt>
180
<dd><p>Exception raised when an error code signifying a permanent error (response
181
codes in the range 500–599) is received.</p>
184
<dl class="exception">
185
<dt id="ftplib.error_proto">
186
<em class="property">exception </em><code class="descclassname">ftplib.</code><code class="descname">error_proto</code><a class="headerlink" href="#ftplib.error_proto" title="Permalink to this definition">¶</a></dt>
187
<dd><p>Exception raised when a reply is received from the server that does not fit
188
the response specifications of the File Transfer Protocol, i.e. begin with a
189
digit in the range 1–5.</p>
193
<dt id="ftplib.all_errors">
194
<code class="descclassname">ftplib.</code><code class="descname">all_errors</code><a class="headerlink" href="#ftplib.all_errors" title="Permalink to this definition">¶</a></dt>
195
<dd><p>The set of all exceptions (as a tuple) that methods of <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a>
196
instances may raise as a result of problems with the FTP connection (as
197
opposed to programming errors made by the caller). This set includes the
198
four exceptions listed above as well as <a class="reference internal" href="socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal"><span class="pre">socket.error</span></code></a> and
199
<a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>.</p>
202
<div class="admonition seealso">
203
<p class="first admonition-title">See also</p>
204
<dl class="docutils">
205
<dt>Module <a class="reference internal" href="netrc.html#module-netrc" title="netrc: Loading of .netrc files."><code class="xref py py-mod docutils literal"><span class="pre">netrc</span></code></a></dt>
206
<dd>Parser for the <code class="file docutils literal"><span class="pre">.netrc</span></code> file format. The file <code class="file docutils literal"><span class="pre">.netrc</span></code> is
207
typically used by FTP clients to load user authentication information
208
before prompting the user.</dd>
210
<p class="last" id="index-3">The file <code class="file docutils literal"><span class="pre">Tools/scripts/ftpmirror.py</span></code> in the Python source distribution is
211
a script that can mirror FTP sites, or portions thereof, using the <a class="reference internal" href="#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">ftplib</span></code></a>
212
module. It can be used as an extended example that applies this module.</p>
214
<div class="section" id="ftp-objects">
215
<span id="id1"></span><h2>20.8.1. FTP Objects<a class="headerlink" href="#ftp-objects" title="Permalink to this headline">¶</a></h2>
216
<p>Several methods are available in two flavors: one for handling text files and
217
another for binary files. These are named for the command which is used
218
followed by <code class="docutils literal"><span class="pre">lines</span></code> for the text version or <code class="docutils literal"><span class="pre">binary</span></code> for the binary version.</p>
219
<p><a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> instances have the following methods:</p>
221
<dt id="ftplib.FTP.set_debuglevel">
222
<code class="descclassname">FTP.</code><code class="descname">set_debuglevel</code><span class="sig-paren">(</span><em>level</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.set_debuglevel" title="Permalink to this definition">¶</a></dt>
223
<dd><p>Set the instance’s debugging level. This controls the amount of debugging
224
output printed. The default, <code class="docutils literal"><span class="pre">0</span></code>, produces no debugging output. A value of
225
<code class="docutils literal"><span class="pre">1</span></code> produces a moderate amount of debugging output, generally a single line
226
per request. A value of <code class="docutils literal"><span class="pre">2</span></code> or higher produces the maximum amount of
227
debugging output, logging each line sent and received on the control connection.</p>
231
<dt id="ftplib.FTP.connect">
232
<code class="descclassname">FTP.</code><code class="descname">connect</code><span class="sig-paren">(</span><em>host</em><span class="optional">[</span>, <em>port</em><span class="optional">[</span>, <em>timeout</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.connect" title="Permalink to this definition">¶</a></dt>
233
<dd><p>Connect to the given host and port. The default port number is <code class="docutils literal"><span class="pre">21</span></code>, as
234
specified by the FTP protocol specification. It is rarely needed to specify a
235
different port number. This function should be called only once for each
236
instance; it should not be called at all if a host was given when the instance
237
was created. All other methods can only be used after a connection has been
239
<p>The optional <em>timeout</em> parameter specifies a timeout in seconds for the
240
connection attempt. If no <em>timeout</em> is passed, the global default timeout
241
setting will be used.</p>
242
<div class="versionchanged">
243
<p><span class="versionmodified">Changed in version 2.6: </span><em>timeout</em> was added.</p>
248
<dt id="ftplib.FTP.getwelcome">
249
<code class="descclassname">FTP.</code><code class="descname">getwelcome</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.getwelcome" title="Permalink to this definition">¶</a></dt>
250
<dd><p>Return the welcome message sent by the server in reply to the initial
251
connection. (This message sometimes contains disclaimers or help information
252
that may be relevant to the user.)</p>
256
<dt id="ftplib.FTP.login">
257
<code class="descclassname">FTP.</code><code class="descname">login</code><span class="sig-paren">(</span><span class="optional">[</span><em>user</em><span class="optional">[</span>, <em>passwd</em><span class="optional">[</span>, <em>acct</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.login" title="Permalink to this definition">¶</a></dt>
258
<dd><p>Log in as the given <em>user</em>. The <em>passwd</em> and <em>acct</em> parameters are optional and
259
default to the empty string. If no <em>user</em> is specified, it defaults to
260
<code class="docutils literal"><span class="pre">'anonymous'</span></code>. If <em>user</em> is <code class="docutils literal"><span class="pre">'anonymous'</span></code>, the default <em>passwd</em> is
261
<code class="docutils literal"><span class="pre">'anonymous@'</span></code>. This function should be called only once for each instance,
262
after a connection has been established; it should not be called at all if a
263
host and user were given when the instance was created. Most FTP commands are
264
only allowed after the client has logged in. The <em>acct</em> parameter supplies
265
“accounting information”; few systems implement this.</p>
269
<dt id="ftplib.FTP.abort">
270
<code class="descclassname">FTP.</code><code class="descname">abort</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.abort" title="Permalink to this definition">¶</a></dt>
271
<dd><p>Abort a file transfer that is in progress. Using this does not always work, but
272
it’s worth a try.</p>
276
<dt id="ftplib.FTP.sendcmd">
277
<code class="descclassname">FTP.</code><code class="descname">sendcmd</code><span class="sig-paren">(</span><em>command</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.sendcmd" title="Permalink to this definition">¶</a></dt>
278
<dd><p>Send a simple command string to the server and return the response string.</p>
282
<dt id="ftplib.FTP.voidcmd">
283
<code class="descclassname">FTP.</code><code class="descname">voidcmd</code><span class="sig-paren">(</span><em>command</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.voidcmd" title="Permalink to this definition">¶</a></dt>
284
<dd><p>Send a simple command string to the server and handle the response. Return
285
nothing if a response code corresponding to success (codes in the range
286
200–299) is received. Raise <a class="reference internal" href="#ftplib.error_reply" title="ftplib.error_reply"><code class="xref py py-exc docutils literal"><span class="pre">error_reply</span></code></a> otherwise.</p>
290
<dt id="ftplib.FTP.retrbinary">
291
<code class="descclassname">FTP.</code><code class="descname">retrbinary</code><span class="sig-paren">(</span><em>command</em>, <em>callback</em><span class="optional">[</span>, <em>maxblocksize</em><span class="optional">[</span>, <em>rest</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.retrbinary" title="Permalink to this definition">¶</a></dt>
292
<dd><p>Retrieve a file in binary transfer mode. <em>command</em> should be an appropriate
293
<code class="docutils literal"><span class="pre">RETR</span></code> command: <code class="docutils literal"><span class="pre">'RETR</span> <span class="pre">filename'</span></code>. The <em>callback</em> function is called for
294
each block of data received, with a single string argument giving the data
295
block. The optional <em>maxblocksize</em> argument specifies the maximum chunk size to
296
read on the low-level socket object created to do the actual transfer (which
297
will also be the largest size of the data blocks passed to <em>callback</em>). A
298
reasonable default is chosen. <em>rest</em> means the same thing as in the
299
<a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a> method.</p>
303
<dt id="ftplib.FTP.retrlines">
304
<code class="descclassname">FTP.</code><code class="descname">retrlines</code><span class="sig-paren">(</span><em>command</em><span class="optional">[</span>, <em>callback</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.retrlines" title="Permalink to this definition">¶</a></dt>
305
<dd><p>Retrieve a file or directory listing in ASCII transfer mode. <em>command</em>
306
should be an appropriate <code class="docutils literal"><span class="pre">RETR</span></code> command (see <a class="reference internal" href="#ftplib.FTP.retrbinary" title="ftplib.FTP.retrbinary"><code class="xref py py-meth docutils literal"><span class="pre">retrbinary()</span></code></a>) or a
307
command such as <code class="docutils literal"><span class="pre">LIST</span></code>, <code class="docutils literal"><span class="pre">NLST</span></code> or <code class="docutils literal"><span class="pre">MLSD</span></code> (usually just the string
308
<code class="docutils literal"><span class="pre">'LIST'</span></code>). <code class="docutils literal"><span class="pre">LIST</span></code> retrieves a list of files and information about those files.
309
<code class="docutils literal"><span class="pre">NLST</span></code> retrieves a list of file names. On some servers, <code class="docutils literal"><span class="pre">MLSD</span></code> retrieves
310
a machine readable list of files and information about those files. The <em>callback</em>
311
function is called for each line with a string argument containing the line with
312
the trailing CRLF stripped. The default <em>callback</em> prints the line to <code class="docutils literal"><span class="pre">sys.stdout</span></code>.</p>
316
<dt id="ftplib.FTP.set_pasv">
317
<code class="descclassname">FTP.</code><code class="descname">set_pasv</code><span class="sig-paren">(</span><em>boolean</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.set_pasv" title="Permalink to this definition">¶</a></dt>
318
<dd><p>Enable “passive” mode if <em>boolean</em> is true, other disable passive mode. (In
319
Python 2.0 and before, passive mode was off by default; in Python 2.1 and later,
320
it is on by default.)</p>
324
<dt id="ftplib.FTP.storbinary">
325
<code class="descclassname">FTP.</code><code class="descname">storbinary</code><span class="sig-paren">(</span><em>command</em>, <em>file</em><span class="optional">[</span>, <em>blocksize</em>, <em>callback</em>, <em>rest</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.storbinary" title="Permalink to this definition">¶</a></dt>
326
<dd><p>Store a file in binary transfer mode. <em>command</em> should be an appropriate
327
<code class="docutils literal"><span class="pre">STOR</span></code> command: <code class="docutils literal"><span class="pre">"STOR</span> <span class="pre">filename"</span></code>. <em>file</em> is an open file object which is
328
read until EOF using its <code class="xref py py-meth docutils literal"><span class="pre">read()</span></code> method in blocks of size <em>blocksize</em> to
329
provide the data to be stored. The <em>blocksize</em> argument defaults to 8192.
330
<em>callback</em> is an optional single parameter callable that is called
331
on each block of data after it is sent. <em>rest</em> means the same thing as in
332
the <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a> method.</p>
333
<div class="versionchanged">
334
<p><span class="versionmodified">Changed in version 2.1: </span>default for <em>blocksize</em> added.</p>
336
<div class="versionchanged">
337
<p><span class="versionmodified">Changed in version 2.6: </span><em>callback</em> parameter added.</p>
339
<div class="versionchanged">
340
<p><span class="versionmodified">Changed in version 2.7: </span><em>rest</em> parameter added.</p>
345
<dt id="ftplib.FTP.storlines">
346
<code class="descclassname">FTP.</code><code class="descname">storlines</code><span class="sig-paren">(</span><em>command</em>, <em>file</em><span class="optional">[</span>, <em>callback</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.storlines" title="Permalink to this definition">¶</a></dt>
347
<dd><p>Store a file in ASCII transfer mode. <em>command</em> should be an appropriate
348
<code class="docutils literal"><span class="pre">STOR</span></code> command (see <a class="reference internal" href="#ftplib.FTP.storbinary" title="ftplib.FTP.storbinary"><code class="xref py py-meth docutils literal"><span class="pre">storbinary()</span></code></a>). Lines are read until EOF from the
349
open file object <em>file</em> using its <a class="reference internal" href="stdtypes.html#file.readline" title="file.readline"><code class="xref py py-meth docutils literal"><span class="pre">readline()</span></code></a> method to provide
350
the data to be stored. <em>callback</em> is an optional single parameter callable
351
that is called on each line after it is sent.</p>
352
<div class="versionchanged">
353
<p><span class="versionmodified">Changed in version 2.6: </span><em>callback</em> parameter added.</p>
358
<dt id="ftplib.FTP.transfercmd">
359
<code class="descclassname">FTP.</code><code class="descname">transfercmd</code><span class="sig-paren">(</span><em>cmd</em><span class="optional">[</span>, <em>rest</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.transfercmd" title="Permalink to this definition">¶</a></dt>
360
<dd><p>Initiate a transfer over the data connection. If the transfer is active, send a
361
<code class="docutils literal"><span class="pre">EPRT</span></code> or <code class="docutils literal"><span class="pre">PORT</span></code> command and the transfer command specified by <em>cmd</em>, and
362
accept the connection. If the server is passive, send a <code class="docutils literal"><span class="pre">EPSV</span></code> or <code class="docutils literal"><span class="pre">PASV</span></code>
363
command, connect to it, and start the transfer command. Either way, return the
364
socket for the connection.</p>
365
<p>If optional <em>rest</em> is given, a <code class="docutils literal"><span class="pre">REST</span></code> command is sent to the server, passing
366
<em>rest</em> as an argument. <em>rest</em> is usually a byte offset into the requested file,
367
telling the server to restart sending the file’s bytes at the requested offset,
368
skipping over the initial bytes. Note however that RFC 959 requires only that
369
<em>rest</em> be a string containing characters in the printable range from ASCII code
370
33 to ASCII code 126. The <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a> method, therefore, converts
371
<em>rest</em> to a string, but no check is performed on the string’s contents. If the
372
server does not recognize the <code class="docutils literal"><span class="pre">REST</span></code> command, an <a class="reference internal" href="#ftplib.error_reply" title="ftplib.error_reply"><code class="xref py py-exc docutils literal"><span class="pre">error_reply</span></code></a> exception
373
will be raised. If this happens, simply call <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a> without a
374
<em>rest</em> argument.</p>
378
<dt id="ftplib.FTP.ntransfercmd">
379
<code class="descclassname">FTP.</code><code class="descname">ntransfercmd</code><span class="sig-paren">(</span><em>cmd</em><span class="optional">[</span>, <em>rest</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.ntransfercmd" title="Permalink to this definition">¶</a></dt>
380
<dd><p>Like <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a>, but returns a tuple of the data connection and the
381
expected size of the data. If the expected size could not be computed, <code class="docutils literal"><span class="pre">None</span></code>
382
will be returned as the expected size. <em>cmd</em> and <em>rest</em> means the same thing as
383
in <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal"><span class="pre">transfercmd()</span></code></a>.</p>
387
<dt id="ftplib.FTP.nlst">
388
<code class="descclassname">FTP.</code><code class="descname">nlst</code><span class="sig-paren">(</span><em>argument</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.nlst" title="Permalink to this definition">¶</a></dt>
389
<dd><p>Return a list of file names as returned by the <code class="docutils literal"><span class="pre">NLST</span></code> command. The
390
optional <em>argument</em> is a directory to list (default is the current server
391
directory). Multiple arguments can be used to pass non-standard options to
392
the <code class="docutils literal"><span class="pre">NLST</span></code> command.</p>
396
<dt id="ftplib.FTP.dir">
397
<code class="descclassname">FTP.</code><code class="descname">dir</code><span class="sig-paren">(</span><em>argument</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.dir" title="Permalink to this definition">¶</a></dt>
398
<dd><p>Produce a directory listing as returned by the <code class="docutils literal"><span class="pre">LIST</span></code> command, printing it to
399
standard output. The optional <em>argument</em> is a directory to list (default is the
400
current server directory). Multiple arguments can be used to pass non-standard
401
options to the <code class="docutils literal"><span class="pre">LIST</span></code> command. If the last argument is a function, it is used
402
as a <em>callback</em> function as for <a class="reference internal" href="#ftplib.FTP.retrlines" title="ftplib.FTP.retrlines"><code class="xref py py-meth docutils literal"><span class="pre">retrlines()</span></code></a>; the default prints to
403
<code class="docutils literal"><span class="pre">sys.stdout</span></code>. This method returns <code class="docutils literal"><span class="pre">None</span></code>.</p>
407
<dt id="ftplib.FTP.rename">
408
<code class="descclassname">FTP.</code><code class="descname">rename</code><span class="sig-paren">(</span><em>fromname</em>, <em>toname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.rename" title="Permalink to this definition">¶</a></dt>
409
<dd><p>Rename file <em>fromname</em> on the server to <em>toname</em>.</p>
413
<dt id="ftplib.FTP.delete">
414
<code class="descclassname">FTP.</code><code class="descname">delete</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.delete" title="Permalink to this definition">¶</a></dt>
415
<dd><p>Remove the file named <em>filename</em> from the server. If successful, returns the
416
text of the response, otherwise raises <a class="reference internal" href="#ftplib.error_perm" title="ftplib.error_perm"><code class="xref py py-exc docutils literal"><span class="pre">error_perm</span></code></a> on permission errors or
417
<a class="reference internal" href="#ftplib.error_reply" title="ftplib.error_reply"><code class="xref py py-exc docutils literal"><span class="pre">error_reply</span></code></a> on other errors.</p>
421
<dt id="ftplib.FTP.cwd">
422
<code class="descclassname">FTP.</code><code class="descname">cwd</code><span class="sig-paren">(</span><em>pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.cwd" title="Permalink to this definition">¶</a></dt>
423
<dd><p>Set the current directory on the server.</p>
427
<dt id="ftplib.FTP.mkd">
428
<code class="descclassname">FTP.</code><code class="descname">mkd</code><span class="sig-paren">(</span><em>pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.mkd" title="Permalink to this definition">¶</a></dt>
429
<dd><p>Create a new directory on the server.</p>
433
<dt id="ftplib.FTP.pwd">
434
<code class="descclassname">FTP.</code><code class="descname">pwd</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.pwd" title="Permalink to this definition">¶</a></dt>
435
<dd><p>Return the pathname of the current directory on the server.</p>
439
<dt id="ftplib.FTP.rmd">
440
<code class="descclassname">FTP.</code><code class="descname">rmd</code><span class="sig-paren">(</span><em>dirname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.rmd" title="Permalink to this definition">¶</a></dt>
441
<dd><p>Remove the directory named <em>dirname</em> on the server.</p>
445
<dt id="ftplib.FTP.size">
446
<code class="descclassname">FTP.</code><code class="descname">size</code><span class="sig-paren">(</span><em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.size" title="Permalink to this definition">¶</a></dt>
447
<dd><p>Request the size of the file named <em>filename</em> on the server. On success, the
448
size of the file is returned as an integer, otherwise <code class="docutils literal"><span class="pre">None</span></code> is returned.
449
Note that the <code class="docutils literal"><span class="pre">SIZE</span></code> command is not standardized, but is supported by many
450
common server implementations.</p>
454
<dt id="ftplib.FTP.quit">
455
<code class="descclassname">FTP.</code><code class="descname">quit</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.quit" title="Permalink to this definition">¶</a></dt>
456
<dd><p>Send a <code class="docutils literal"><span class="pre">QUIT</span></code> command to the server and close the connection. This is the
457
“polite” way to close a connection, but it may raise an exception if the server
458
responds with an error to the <code class="docutils literal"><span class="pre">QUIT</span></code> command. This implies a call to the
459
<a class="reference internal" href="#ftplib.FTP.close" title="ftplib.FTP.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> method which renders the <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> instance useless for
460
subsequent calls (see below).</p>
464
<dt id="ftplib.FTP.close">
465
<code class="descclassname">FTP.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.close" title="Permalink to this definition">¶</a></dt>
466
<dd><p>Close the connection unilaterally. This should not be applied to an already
467
closed connection such as after a successful call to <a class="reference internal" href="#ftplib.FTP.quit" title="ftplib.FTP.quit"><code class="xref py py-meth docutils literal"><span class="pre">quit()</span></code></a>.
468
After this call the <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a> instance should not be used any more (after
469
a call to <a class="reference internal" href="#ftplib.FTP.close" title="ftplib.FTP.close"><code class="xref py py-meth docutils literal"><span class="pre">close()</span></code></a> or <a class="reference internal" href="#ftplib.FTP.quit" title="ftplib.FTP.quit"><code class="xref py py-meth docutils literal"><span class="pre">quit()</span></code></a> you cannot reopen the
470
connection by issuing another <a class="reference internal" href="#ftplib.FTP.login" title="ftplib.FTP.login"><code class="xref py py-meth docutils literal"><span class="pre">login()</span></code></a> method).</p>
474
<div class="section" id="ftp-tls-objects">
475
<h2>20.8.2. FTP_TLS Objects<a class="headerlink" href="#ftp-tls-objects" title="Permalink to this headline">¶</a></h2>
476
<p><a class="reference internal" href="#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal"><span class="pre">FTP_TLS</span></code></a> class inherits from <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal"><span class="pre">FTP</span></code></a>, defining these additional objects:</p>
477
<dl class="attribute">
478
<dt id="ftplib.FTP_TLS.ssl_version">
479
<code class="descclassname">FTP_TLS.</code><code class="descname">ssl_version</code><a class="headerlink" href="#ftplib.FTP_TLS.ssl_version" title="Permalink to this definition">¶</a></dt>
480
<dd><p>The SSL version to use (defaults to <a class="reference internal" href="ssl.html#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-attr docutils literal"><span class="pre">ssl.PROTOCOL_SSLv23</span></code></a>).</p>
484
<dt id="ftplib.FTP_TLS.auth">
485
<code class="descclassname">FTP_TLS.</code><code class="descname">auth</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.auth" title="Permalink to this definition">¶</a></dt>
486
<dd><p>Set up secure control connection by using TLS or SSL, depending on what
487
specified in <a class="reference internal" href="#ftplib.FTP_TLS.ssl_version" title="ftplib.FTP_TLS.ssl_version"><code class="xref py py-meth docutils literal"><span class="pre">ssl_version()</span></code></a> attribute.</p>
491
<dt id="ftplib.FTP_TLS.prot_p">
492
<code class="descclassname">FTP_TLS.</code><code class="descname">prot_p</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.prot_p" title="Permalink to this definition">¶</a></dt>
493
<dd><p>Set up secure data connection.</p>
497
<dt id="ftplib.FTP_TLS.prot_c">
498
<code class="descclassname">FTP_TLS.</code><code class="descname">prot_c</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.prot_c" title="Permalink to this definition">¶</a></dt>
499
<dd><p>Set up clear text data connection.</p>
509
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
510
<div class="sphinxsidebarwrapper">
511
<h3><a href="../contents.html">Table Of Contents</a></h3>
513
<li><a class="reference internal" href="#">20.8. <code class="docutils literal"><span class="pre">ftplib</span></code> — FTP protocol client</a><ul>
514
<li><a class="reference internal" href="#ftp-objects">20.8.1. FTP Objects</a></li>
515
<li><a class="reference internal" href="#ftp-tls-objects">20.8.2. FTP_TLS Objects</a></li>
520
<h4>Previous topic</h4>
521
<p class="topless"><a href="httplib.html"
522
title="previous chapter">20.7. <code class="docutils literal"><span class="pre">httplib</span></code> — HTTP protocol client</a></p>
524
<p class="topless"><a href="poplib.html"
525
title="next chapter">20.9. <code class="docutils literal"><span class="pre">poplib</span></code> — POP3 protocol client</a></p>
527
<ul class="this-page-menu">
528
<li><a href="../bugs.html">Report a Bug</a></li>
529
<li><a href="../_sources/library/ftplib.txt"
530
rel="nofollow">Show Source</a></li>
533
<div id="searchbox" style="display: none" role="search">
534
<h3>Quick search</h3>
535
<form class="search" action="../search.html" method="get">
536
<input type="text" name="q" />
537
<input type="submit" value="Go" />
538
<input type="hidden" name="check_keywords" value="yes" />
539
<input type="hidden" name="area" value="default" />
541
<p class="searchtip" style="font-size: 90%">
542
Enter search terms or a module, class or function name.
545
<script type="text/javascript">$('#searchbox').show(0);</script>
548
<div class="clearer"></div>
550
<div class="related" role="navigation" aria-label="related navigation">
553
<li class="right" style="margin-right: 10px">
554
<a href="../genindex.html" title="General Index"
557
<a href="../py-modindex.html" title="Python Module Index"
560
<a href="poplib.html" title="20.9. poplib — POP3 protocol client"
563
<a href="httplib.html" title="20.7. httplib — HTTP protocol client"
565
<li><img src="../_static/py.png" alt=""
566
style="vertical-align: middle; margin-top: -1px"/></li>
567
<li><a href="https://www.python.org/">Python</a> »</li>
569
<span class="version_switcher_placeholder">2.7.11</span>
570
<a href="../index.html">Documentation</a> »
573
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
574
<li class="nav-item nav-item-2"><a href="internet.html" >20. Internet Protocols and Support</a> »</li>
578
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
580
The Python Software Foundation is a non-profit corporation.
581
<a href="https://www.python.org/psf/donations/">Please donate.</a>
583
Last updated on Jan 23, 2016.
584
<a href="../bugs.html">Found a bug</a>?
586
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'