636 lines
59 KiB
HTML
636 lines
59 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>ftplib --- FTP 协议客户端 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="poplib --- POP3 协议客户端" href="poplib.html" />
|
||
<link rel="prev" title="http.client --- HTTP 协议客户端" href="http.client.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/ftplib.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code> --- FTP 协议客户端</a><ul>
|
||
<li><a class="reference internal" href="#ftp-objects">FTP 对象</a></li>
|
||
<li><a class="reference internal" href="#ftp-tls-objects">FTP_TLS 对象</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="http.client.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code> --- HTTP 协议客户端</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="poplib.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code> --- POP3 协议客户端</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/ftplib.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/ftplib.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="poplib.html" title="poplib --- POP3 协议客户端"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="http.client.html" title="http.client --- HTTP 协议客户端"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">互联网协议和支持</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-ftplib">
|
||
<span id="ftplib-ftp-protocol-client"></span><h1><a class="reference internal" href="#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> --- FTP 协议客户端<a class="headerlink" href="#module-ftplib" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/ftplib.py">Lib/ftplib.py</a></p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p>本模块定义了 <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 类和一些相关项目。<a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 类实现了 FTP 协议的客户端。可以用该类编写 Python 程序,执行各种自动化的 FTP 任务,如镜像其他 FTP 服务器。<a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a> 模块也用它来处理使用了 FTP 的 URL。关于 FTP(文件传输协议)的详情请参阅 Internet <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc959.html"><strong>RFC 959</strong></a>。</p>
|
||
<p>以下是使用 <a class="reference internal" href="#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> 模块的会话示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP</span>
|
||
<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>
|
||
<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>
|
||
<span class="go">'230 Login successful.'</span>
|
||
<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>
|
||
<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>
|
||
<span class="go">-rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README</span>
|
||
<span class="go">...</span>
|
||
<span class="go">drwxr-sr-x 5 1176 1176 4096 Dec 19 2000 pool</span>
|
||
<span class="go">drwxr-sr-x 4 1176 1176 4096 Nov 17 2008 project</span>
|
||
<span class="go">drwxr-xr-x 3 1176 1176 4096 Oct 10 2012 tools</span>
|
||
<span class="go">'226 Directory send OK.'</span>
|
||
<span class="gp">>>> </span><span class="k">with</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="k">as</span> <span class="n">fp</span><span class="p">:</span>
|
||
<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="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">)</span>
|
||
<span class="go">'226 Transfer complete.'</span>
|
||
<span class="gp">>>> </span><span class="n">ftp</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这个模块定义了以下内容:</p>
|
||
<dl class="class">
|
||
<dt id="ftplib.FTP">
|
||
<em class="property">class </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">FTP</code><span class="sig-paren">(</span><em class="sig-param">host=''</em>, <em class="sig-param">user=''</em>, <em class="sig-param">passwd=''</em>, <em class="sig-param">acct=''</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP" title="永久链接至目标">¶</a></dt>
|
||
<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 notranslate"><span class="pre">FTP</span></code></a> class. When <em>host</em> is given, the
|
||
method call <code class="docutils literal notranslate"><span class="pre">connect(host)</span></code> is made. When <em>user</em> is given, additionally
|
||
the method call <code class="docutils literal notranslate"><span class="pre">login(user,</span> <span class="pre">passwd,</span> <span class="pre">acct)</span></code> is made (where <em>passwd</em> and
|
||
<em>acct</em> default to the empty string when not given). The optional <em>timeout</em>
|
||
parameter specifies a timeout in seconds for blocking operations like the
|
||
connection attempt (if is not specified, the global default timeout setting
|
||
will be used). <em>source_address</em> is a 2-tuple <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> for the socket
|
||
to bind to as its source address before connecting.</p>
|
||
<p><a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 类支持 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句,例如:</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="kn">import</span> <span class="n">FTP</span>
|
||
<span class="gp">>>> </span><span class="k">with</span> <span class="n">FTP</span><span class="p">(</span><span class="s2">"ftp1.at.proftpd.org"</span><span class="p">)</span> <span class="k">as</span> <span class="n">ftp</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="n">ftp</span><span class="o">.</span><span class="n">login</span><span class="p">()</span>
|
||
<span class="gp">... </span> <span class="n">ftp</span><span class="o">.</span><span class="n">dir</span><span class="p">()</span>
|
||
<span class="gp">... </span>
|
||
<span class="go">'230 Anonymous login ok, restrictions apply.'</span>
|
||
<span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .</span>
|
||
<span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 ..</span>
|
||
<span class="go">dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS</span>
|
||
<span class="go">dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora</span>
|
||
<span class="gp">>>></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了对 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句的支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>source_address</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="ftplib.FTP_TLS">
|
||
<em class="property">class </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">FTP_TLS</code><span class="sig-paren">(</span><em class="sig-param">host=''</em>, <em class="sig-param">user=''</em>, <em class="sig-param">passwd=''</em>, <em class="sig-param">acct=''</em>, <em class="sig-param">keyfile=None</em>, <em class="sig-param">certfile=None</em>, <em class="sig-param">context=None</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>一个 <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 的子类,它为 FTP 添加了 TLS 支持,如 <span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4217.html"><strong>RFC 4217</strong></a> 所述。它将像通常一样连接到 21 端口,暗中保护在身份验证前的 FTP 控制连接。而保护数据连接需要用户明确调用 <a class="reference internal" href="#ftplib.FTP_TLS.prot_p" title="ftplib.FTP_TLS.prot_p"><code class="xref py py-meth docutils literal notranslate"><span class="pre">prot_p()</span></code></a> 方法。<em>context</em> 是一个 <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 对象,该对象可以将 SSL 配置选项、证书和私钥打包放入一个单独的(可以长久存在的)结构中。请阅读 <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">安全考量</span></a> 以获取最佳实践。</p>
|
||
<p><em>keyfile</em> 和 <em>certfile</em> 是可以代替 <em>context</em> 的传统方案,它们可以分别指向 PEM 格式的私钥和证书链文件,用于进行 SSL 连接。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>source_address</em> 参数。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>本类现在支持使用 <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> 和 <em>服务器名称指示</em> (参阅 <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a>)进行主机名检查。</p>
|
||
</div>
|
||
<div class="deprecated">
|
||
<p><span class="versionmodified deprecated">3.6 版后已移除: </span><em>keyfile</em> 和 <em>certfile</em> 已弃用并转而推荐 <em>context</em>。 请改用 <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> 或让 <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> 为你选择系统所信任的 CA 证书。</p>
|
||
</div>
|
||
<p>以下是使用 <a class="reference internal" href="#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP_TLS</span></code></a> 类的会话示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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.pureftpd.org'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">login</span><span class="p">()</span>
|
||
<span class="go">'230 Anonymous user logged in'</span>
|
||
<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="go">'200 Data protection level set to "private"'</span>
|
||
<span class="gp">>>> </span><span class="n">ftps</span><span class="o">.</span><span class="n">nlst</span><span class="p">()</span>
|
||
<span class="go">['6jack', 'OpenBSD', 'antilink', 'blogbench', 'bsdcam', 'clockspeed', 'djbdns-jedi', 'docs', 'eaccelerator-jedi', 'favicon.ico', 'francotone', 'fugu', 'ignore', 'libpuzzle', 'metalog', 'minidentd', 'misc', 'mysql-udf-global-user-variables', 'php-jenkins-hash', 'php-skein-hash', 'php-webdav', 'phpaudit', 'phpbench', 'pincaster', 'ping', 'posto', 'pub', 'public', 'public_keys', 'pure-ftpd', 'qscan', 'qtc', 'sharedance', 'skycache', 'sound', 'tmp', 'ucarp']</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="ftplib.error_reply">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">error_reply</code><a class="headerlink" href="#ftplib.error_reply" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从服务器收到意外答复时,将引发本异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="ftplib.error_temp">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">error_temp</code><a class="headerlink" href="#ftplib.error_temp" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>收到表示临时错误的错误代码(响应代码在 400--499 范围内)时,将引发本异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="ftplib.error_perm">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">error_perm</code><a class="headerlink" href="#ftplib.error_perm" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>收到表示永久性错误的错误代码(响应代码在 500--599 范围内)时,将引发本异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="ftplib.error_proto">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">error_proto</code><a class="headerlink" href="#ftplib.error_proto" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从服务器收到不符合 FTP 响应规范的答复,比如以数字 1--5 开头时,将引发本异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="ftplib.all_errors">
|
||
<code class="sig-prename descclassname">ftplib.</code><code class="sig-name descname">all_errors</code><a class="headerlink" href="#ftplib.all_errors" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>所有异常的集合(一个元组),由于 FTP 连接出现问题(并非调用者的编码错误),<a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 实例的方法可能会引发这些异常。该集合包括上面列出的四个异常以及 <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 和 <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt><a class="reference internal" href="netrc.html#module-netrc" title="netrc: Loading of .netrc files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">netrc</span></code></a> 模块</dt><dd><p><code class="file docutils literal notranslate"><span class="pre">.netrc</span></code> 文件格式解析器。FTP 客户端在响应用户之前,通常使用 <code class="file docutils literal notranslate"><span class="pre">.netrc</span></code> 文件来加载用户认证信息。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
<section id="ftp-objects">
|
||
<span id="id1"></span><h2>FTP 对象<a class="headerlink" href="#ftp-objects" title="永久链接至标题">¶</a></h2>
|
||
<p>一些方法可以按照两种方式来使用:一种处理文本文件,另一种处理二进制文件。方法名称与相应的命令相同,文本版中命令后面跟着 <code class="docutils literal notranslate"><span class="pre">lines</span></code>,二进制版中命令后面跟着 <code class="docutils literal notranslate"><span class="pre">binary</span></code>。</p>
|
||
<p><a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 实例具有下列方法:</p>
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.set_debuglevel">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">set_debuglevel</code><span class="sig-paren">(</span><em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.set_debuglevel" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>设置实例的调试级别,它控制着调试信息的数量。默认值 <code class="docutils literal notranslate"><span class="pre">0</span></code> 不产生调试信息。值 <code class="docutils literal notranslate"><span class="pre">1</span></code> 产生中等数量的调试信息,通常每个请求产生一行。大于或等于 <code class="docutils literal notranslate"><span class="pre">2</span></code> 的值产生的调试信息最多,FTP 控制连接上发送和接收的每一行都将被记录下来。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.connect">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">connect</code><span class="sig-paren">(</span><em class="sig-param">host=''</em>, <em class="sig-param">port=0</em>, <em class="sig-param">timeout=None</em>, <em class="sig-param">source_address=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.connect" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>连接到给定的主机和端口。默认端口号由 FTP 协议规范规定,为 <code class="docutils literal notranslate"><span class="pre">21</span></code>。偶尔才需要指定其他端口号。每个实例只应调用一次本函数,如果在创建实例时就传入了 host,则根本不应调用它。所有其他方法只能在建立连接后使用。可选参数 <em>timeout</em> 指定连接尝试的超时(以秒为单位)。如果没有传入 <em>timeout</em>,将使用全局默认超时设置。<em>source_address</em> 是一个 2 元组 <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code>,套接字在连接前绑定它,作为其源地址。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">ftplib.connect</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">host</span></code>, <code class="docutils literal notranslate"><span class="pre">port</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>source_address</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.getwelcome">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">getwelcome</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.getwelcome" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回服务器发送的欢迎消息,作为连接开始的回复。(该消息有时包含与用户有关的免责声明或帮助信息。)</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.login">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">login</code><span class="sig-paren">(</span><em class="sig-param">user='anonymous'</em>, <em class="sig-param">passwd=''</em>, <em class="sig-param">acct=''</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.login" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>以 <em>user</em> 的身份登录。<em>passwd</em> 和 <em>acct</em> 是可选参数,默认为空字符串。如果没有指定 <em>user</em>,则默认为 <code class="docutils literal notranslate"><span class="pre">'anonymous'</span></code>。如果 <em>user</em> 为 <code class="docutils literal notranslate"><span class="pre">'anonymous'</span></code>,那么默认的 <em>passwd</em> 是 <code class="docutils literal notranslate"><span class="pre">'anonymous@'</span></code>。连接建立后,每个实例只应调用一次本函数;如果在创建实例时传入了 host 和 user,则完全不应该调用本函数。在客户端登录后,才允许执行大多数 FTP 命令。<em>acct</em> 参数提供记账信息 ("accounting information");仅少数系统实现了该特性。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.abort">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">abort</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.abort" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>中止正在进行的文件传输。本方法并不总是有效,但值得一试。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.sendcmd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">sendcmd</code><span class="sig-paren">(</span><em class="sig-param">cmd</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.sendcmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将一条简单的命令字符串发送到服务器,返回响应的字符串。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">ftplib.sendcmd</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">cmd</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.voidcmd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">voidcmd</code><span class="sig-paren">(</span><em class="sig-param">cmd</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.voidcmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将一条简单的命令字符串发送到服务器,并处理响应内容。如果收到的响应代码表示的是成功(代码范围 200--299),则不返回任何内容。否则将引发 <a class="reference internal" href="#ftplib.error_reply" title="ftplib.error_reply"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error_reply</span></code></a>。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">ftplib.sendcmd</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">cmd</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.retrbinary">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">retrbinary</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">callback</em>, <em class="sig-param">blocksize=8192</em>, <em class="sig-param">rest=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.retrbinary" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>以二进制传输模式下载文件。<em>cmd</em> 应为恰当的 <code class="docutils literal notranslate"><span class="pre">RETR</span></code> 命令:<code class="docutils literal notranslate"><span class="pre">'RETR</span> <span class="pre">文件名'</span></code>。<em>callback</em> 函数会在收到每个数据块时调用,传入的参数是表示数据块的一个字节。为执行实际传输,创建了底层套接字对象,可选参数 <em>blocksize</em> 指定了读取该对象时的最大块大小(这也是传入 <em>callback</em> 的数据块的最大大小)。已经选择了合理的默认值。<em>rest</em> 的含义与 <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transfercmd()</span></code></a> 方法中的含义相同。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.retrlines">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">retrlines</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">callback=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.retrlines" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>Retrieve a file or directory listing in ASCII transfer mode. <em>cmd</em> should be
|
||
an appropriate <code class="docutils literal notranslate"><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 notranslate"><span class="pre">retrbinary()</span></code></a>) or a command such as
|
||
<code class="docutils literal notranslate"><span class="pre">LIST</span></code> or <code class="docutils literal notranslate"><span class="pre">NLST</span></code> (usually just the string <code class="docutils literal notranslate"><span class="pre">'LIST'</span></code>).
|
||
<code class="docutils literal notranslate"><span class="pre">LIST</span></code> retrieves a list of files and information about those files.
|
||
<code class="docutils literal notranslate"><span class="pre">NLST</span></code> retrieves a list of file names.
|
||
The <em>callback</em> function is called for each line with a string argument
|
||
containing the line with the trailing CRLF stripped. The default <em>callback</em>
|
||
prints the line to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.set_pasv">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">set_pasv</code><span class="sig-paren">(</span><em class="sig-param">val</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.set_pasv" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>val</em> 为 true,则打开“被动”模式,否则禁用被动模式。默认下被动模式是打开的。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.storbinary">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">storbinary</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">fp</em>, <em class="sig-param">blocksize=8192</em>, <em class="sig-param">callback=None</em>, <em class="sig-param">rest=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.storbinary" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>以二进制传输模式存储文件。 <em>cmd</em> 应为恰当的 <code class="docutils literal notranslate"><span class="pre">STOR</span></code> 命令: <code class="docutils literal notranslate"><span class="pre">"STOR</span> <span class="pre">filename"</span></code>。<em>fp</em> 是一个 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件对象</span></a> (以二进制模式打开),将使用它的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 方法读取它,用于提供要存储的数据,直到遇到 EOF,读取时的块大小为 <em>blocksize</em>。 参数 <em>blocksize</em> 的默认值为 8192。 可选参数 <em>callback</em> 是单参数函数,在每个数据块发送后都会以该数据块作为参数来调用它。<em>rest</em> 的含义与 <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transfercmd()</span></code></a> 方法中的含义相同。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了 <em>rest</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.storlines">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">storlines</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">fp</em>, <em class="sig-param">callback=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.storlines" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>Store a file in ASCII transfer mode. <em>cmd</em> should be an appropriate
|
||
<code class="docutils literal notranslate"><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 notranslate"><span class="pre">storbinary()</span></code></a>). Lines are read until EOF from the
|
||
<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>fp</em> (opened in binary mode) using its <a class="reference internal" href="io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a>
|
||
method to provide the data to be stored. <em>callback</em> is an optional single
|
||
parameter callable that is called on each line after it is sent.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.transfercmd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">transfercmd</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">rest=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.transfercmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在 FTP 数据连接上开始传输数据。如果传输处于活动状态,传输命令由 <em>cmd</em> 指定,需发送 <code class="docutils literal notranslate"><span class="pre">EPRT</span></code> 或 <code class="docutils literal notranslate"><span class="pre">PORT</span></code> 命令,然后接受连接 (accept)。如果服务器是被动服务器,需发送 <code class="docutils literal notranslate"><span class="pre">EPSV</span></code> 或 <code class="docutils literal notranslate"><span class="pre">PASV</span></code> 命令,连接到服务器 (connect),然后启动传输命令。两种方式都将返回用于连接的套接字。</p>
|
||
<p>If optional <em>rest</em> is given, a <code class="docutils literal notranslate"><span class="pre">REST</span></code> command is sent to the server, passing
|
||
<em>rest</em> as an argument. <em>rest</em> 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 <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc959.html"><strong>RFC 959</strong></a> requires only that
|
||
<em>rest</em> be a string containing characters in the printable range from ASCII code
|
||
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 notranslate"><span class="pre">transfercmd()</span></code></a> method, therefore, converts
|
||
<em>rest</em> to a string, but no check is performed on the string's contents. If the
|
||
server does not recognize the <code class="docutils literal notranslate"><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 notranslate"><span class="pre">error_reply</span></code></a> exception
|
||
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 notranslate"><span class="pre">transfercmd()</span></code></a> without a
|
||
<em>rest</em> argument.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.ntransfercmd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">ntransfercmd</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">rest=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.ntransfercmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>类似于 <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transfercmd()</span></code></a>,但返回一个元组,包括数据连接和数据的预计大小。如果预计大小无法计算,则返回的预计大小为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。<em>cmd</em> 和 <em>rest</em> 的含义与 <a class="reference internal" href="#ftplib.FTP.transfercmd" title="ftplib.FTP.transfercmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transfercmd()</span></code></a> 中的相同。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.mlsd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">mlsd</code><span class="sig-paren">(</span><em class="sig-param">path=""</em>, <em class="sig-param">facts=[]</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.mlsd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>使用 <code class="docutils literal notranslate"><span class="pre">MLSD</span></code> 命令以标准格式列出目录内容 (<span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3659.html"><strong>RFC 3659</strong></a>)。如果省略 <em>path</em> 则使用当前目录。<em>facts</em> 是字符串列表,表示所需的信息类型(如 <code class="docutils literal notranslate"><span class="pre">["type",</span> <span class="pre">"size",</span> <span class="pre">"perm"]</span></code>)。返回一个生成器对象,每个在 path 中找到的文件都将在该对象中生成两个元素的元组。第一个元素是文件名,第二个元素是该文件的 facts 的字典。该字典的内容受 <em>facts</em> 参数限制,但不能保证服务器会返回所有请求的 facts。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.nlst">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">nlst</code><span class="sig-paren">(</span><em class="sig-param">argument</em><span class="optional">[</span>, <em class="sig-param">...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.nlst" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回一个文件名列表,文件名由 <code class="docutils literal notranslate"><span class="pre">NLST</span></code> 命令返回。可选参数 <em>argument</em> 是待列出的目录(默认为当前服务器目录)。可以使用多个参数,将非标准选项传递给 <code class="docutils literal notranslate"><span class="pre">NLST</span></code> 命令。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果目标服务器支持相关命令,那么 <a class="reference internal" href="#ftplib.FTP.mlsd" title="ftplib.FTP.mlsd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mlsd()</span></code></a> 提供的 API 更好。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.dir">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">dir</code><span class="sig-paren">(</span><em class="sig-param">argument</em><span class="optional">[</span>, <em class="sig-param">...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.dir" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>生成目录列表,即 <code class="docutils literal notranslate"><span class="pre">LIST</span></code> 命令所返回的结果,并将其打印到标准输出。可选参数 <em>argument</em> 是待列出的目录(默认为当前服务器目录)。可以使用多个参数,将非标准选项传递给 <code class="docutils literal notranslate"><span class="pre">LIST</span></code> 命令。如果最后一个参数是一个函数,它将被用作 <em>callback</em> 函数,与 <a class="reference internal" href="#ftplib.FTP.retrlines" title="ftplib.FTP.retrlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">retrlines()</span></code></a> 中的相同,默认将打印到 <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>。本方法返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果目标服务器支持相关命令,那么 <a class="reference internal" href="#ftplib.FTP.mlsd" title="ftplib.FTP.mlsd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mlsd()</span></code></a> 提供的 API 更好。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.rename">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">rename</code><span class="sig-paren">(</span><em class="sig-param">fromname</em>, <em class="sig-param">toname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.rename" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将服务器上的文件 <em>fromname</em> 重命名为 <em>toname</em>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.delete">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">delete</code><span class="sig-paren">(</span><em class="sig-param">filename</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.delete" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将服务器上名为 <em>filename</em> 的文件删除。如果删除成功,返回响应文本,如果删除失败,在权限错误时引发 <a class="reference internal" href="#ftplib.error_perm" title="ftplib.error_perm"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error_perm</span></code></a>,在其他错误时引发 <a class="reference internal" href="#ftplib.error_reply" title="ftplib.error_reply"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error_reply</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.cwd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">cwd</code><span class="sig-paren">(</span><em class="sig-param">pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.cwd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>设置服务器端的当前目录。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.mkd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">mkd</code><span class="sig-paren">(</span><em class="sig-param">pathname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.mkd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在服务器上创建一个新目录。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.pwd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">pwd</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.pwd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回服务器上当前目录的路径。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.rmd">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">rmd</code><span class="sig-paren">(</span><em class="sig-param">dirname</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.rmd" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将服务器上名为 <em>dirname</em> 的目录删除。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.size">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">size</code><span class="sig-paren">(</span><em class="sig-param">filename</em><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.size" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>请求服务器上名为 <em>filename</em> 的文件大小。成功后以整数返回文件大小,未成功则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。注意,<code class="docutils literal notranslate"><span class="pre">SIZE</span></code> 不是标准命令,但通常许多服务器的实现都支持该命令。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.quit">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">quit</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.quit" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>向服务器发送 <code class="docutils literal notranslate"><span class="pre">QUIT</span></code> 命令并关闭连接。 这是关闭一个连接的“礼貌”方式,但是如果服务器对 <code class="docutils literal notranslate"><span class="pre">QUIT</span></code> 命令的响应带有错误消息则这会引发一个异常。 这意味着对 <a class="reference internal" href="#ftplib.FTP.close" title="ftplib.FTP.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> 方法的调用,它将使得 <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 实例对后继调用无效(见下文)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP.close">
|
||
<code class="sig-prename descclassname">FTP.</code><code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP.close" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>单方面关闭连接。 这不该被应用于已经关闭的连接,例如成功调用 <a class="reference internal" href="#ftplib.FTP.quit" title="ftplib.FTP.quit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">quit()</span></code></a> 之后的连接。 在此调用之后 <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a> 实例不应被继续使用(在调用 <a class="reference internal" href="#ftplib.FTP.close" title="ftplib.FTP.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> 或 <a class="reference internal" href="#ftplib.FTP.quit" title="ftplib.FTP.quit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">quit()</span></code></a> 之后你不能通过再次发起调用 <a class="reference internal" href="#ftplib.FTP.login" title="ftplib.FTP.login"><code class="xref py py-meth docutils literal notranslate"><span class="pre">login()</span></code></a> 方法重新打开连接)。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="ftp-tls-objects">
|
||
<h2>FTP_TLS 对象<a class="headerlink" href="#ftp-tls-objects" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP_TLS</span></code></a> 类继承自 <a class="reference internal" href="#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP</span></code></a>,它定义了下述其他对象:</p>
|
||
<dl class="attribute">
|
||
<dt id="ftplib.FTP_TLS.ssl_version">
|
||
<code class="sig-prename descclassname">FTP_TLS.</code><code class="sig-name descname">ssl_version</code><a class="headerlink" href="#ftplib.FTP_TLS.ssl_version" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>欲采用的 SSL 版本(默认为 <a class="reference internal" href="ssl.html#ssl.PROTOCOL_SSLv23" title="ssl.PROTOCOL_SSLv23"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.PROTOCOL_SSLv23</span></code></a>)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP_TLS.auth">
|
||
<code class="sig-prename descclassname">FTP_TLS.</code><code class="sig-name descname">auth</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.auth" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>通过使用 TLS 或 SSL 来设置一个安全控制连接,具体取决于 <a class="reference internal" href="#ftplib.FTP_TLS.ssl_version" title="ftplib.FTP_TLS.ssl_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl_version</span></code></a> 属性是如何设置的。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>此方法现在支持使用 <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> 和 <em>服务器名称指示</em> (参见 <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a>) 进行主机名检查。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP_TLS.ccc">
|
||
<code class="sig-prename descclassname">FTP_TLS.</code><code class="sig-name descname">ccc</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.ccc" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将控制通道回复为纯文本。 这适用于发挥知道如何使用非安全 FTP 处理 NAT 而无需打开固定端口的防火墙的优势。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP_TLS.prot_p">
|
||
<code class="sig-prename descclassname">FTP_TLS.</code><code class="sig-name descname">prot_p</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.prot_p" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>设置加密数据连接。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="ftplib.FTP_TLS.prot_c">
|
||
<code class="sig-prename descclassname">FTP_TLS.</code><code class="sig-name descname">prot_c</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ftplib.FTP_TLS.prot_c" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>设置明文数据连接。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code> --- FTP 协议客户端</a><ul>
|
||
<li><a class="reference internal" href="#ftp-objects">FTP 对象</a></li>
|
||
<li><a class="reference internal" href="#ftp-tls-objects">FTP_TLS 对象</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="http.client.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code> --- HTTP 协议客户端</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="poplib.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code> --- POP3 协议客户端</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/ftplib.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="poplib.html" title="poplib --- POP3 协议客户端"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="http.client.html" title="http.client --- HTTP 协议客户端"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="internet.html" >互联网协议和支持</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |