cpython/Doc/library/ftplib.rst

467 lines
18 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:22 -03:00
:mod:`ftplib` --- FTP protocol client
=====================================
.. module:: ftplib
:synopsis: FTP protocol client (requires sockets).
**Source code:** :source:`Lib/ftplib.py`
2007-08-15 11:28:22 -03:00
.. index::
pair: FTP; protocol
single: FTP; ftplib (standard module)
2011-01-27 16:38:46 -04:00
--------------
2007-08-15 11:28:22 -03:00
This module defines the class :class:`FTP` and a few related items. The
:class:`FTP` class implements the client side of the FTP protocol. You can use
this to write Python programs that perform a variety of automated FTP jobs, such
as mirroring other FTP servers. It is also used by the module
:mod:`urllib.request` to handle URLs that use FTP. For more information on FTP
(File Transfer Protocol), see internet :rfc:`959`.
2007-08-15 11:28:22 -03:00
The default encoding is UTF-8, following :rfc:`2640`.
.. include:: ../includes/wasm-notavail.rst
2007-08-15 11:28:22 -03:00
Here's a sample session using the :mod:`ftplib` module::
>>> from ftplib import FTP
>>> ftp = FTP('ftp.us.debian.org') # connect to host, default port
>>> ftp.login() # user anonymous, passwd anonymous@
'230 Login successful.'
>>> ftp.cwd('debian') # change into "debian" directory
'250 Directory successfully changed.'
>>> ftp.retrlines('LIST') # list directory contents
-rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README
...
drwxr-sr-x 5 1176 1176 4096 Dec 19 2000 pool
drwxr-sr-x 4 1176 1176 4096 Nov 17 2008 project
drwxr-xr-x 3 1176 1176 4096 Oct 10 2012 tools
'226 Directory send OK.'
>>> with open('README', 'wb') as fp:
>>> ftp.retrbinary('RETR README', fp.write)
2007-08-15 11:28:22 -03:00
'226 Transfer complete.'
>>> ftp.quit()
'221 Goodbye.'
2007-08-15 11:28:22 -03:00
.. _ftplib-reference:
Reference
---------
.. _ftp-objects:
FTP objects
^^^^^^^^^^^
2007-08-15 11:28:22 -03:00
.. class:: FTP(host='', user='', passwd='', acct='', timeout=None, source_address=None, *, encoding='utf-8')
2007-08-15 11:28:22 -03:00
Return a new instance of the :class:`FTP` class. When *host* is given, the
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62998 | andrew.kuchling | 2008-05-10 15:51:55 -0400 (Sat, 10 May 2008) | 7 lines #1858 from Tarek Ziade: Allow multiple repositories in .pypirc; see http://wiki.python.org/moin/EnhancedPyPI for discussion. The patch is slightly revised from Tarek's last patch: I've simplified the PyPIRCCommand.finalize_options() method to not look at sys.argv. Tests still pass. ........ r63000 | alexandre.vassalotti | 2008-05-10 15:59:16 -0400 (Sat, 10 May 2008) | 5 lines Cleaned up io._BytesIO.write(). I am amazed that the old code, for inserting null-bytes, actually worked. Who wrote that thing? Oh, it is me... doh. ........ r63002 | brett.cannon | 2008-05-10 16:52:01 -0400 (Sat, 10 May 2008) | 2 lines Revert r62998 as it broke the build (seems distutils.config is missing). ........ r63014 | andrew.kuchling | 2008-05-10 18:12:38 -0400 (Sat, 10 May 2008) | 1 line #1858: add distutils.config module ........ r63027 | brett.cannon | 2008-05-10 21:09:32 -0400 (Sat, 10 May 2008) | 2 lines Flesh out the 3.0 deprecation to suggest using the ctypes module. ........ r63028 | skip.montanaro | 2008-05-10 22:59:30 -0400 (Sat, 10 May 2008) | 4 lines Copied two versions of the example from the interactive session. Delete one. ........ r63037 | georg.brandl | 2008-05-11 03:02:17 -0400 (Sun, 11 May 2008) | 2 lines reload() takes the module itself. ........ r63038 | alexandre.vassalotti | 2008-05-11 03:06:04 -0400 (Sun, 11 May 2008) | 4 lines Added test framework for handling module renames. Factored the import guard in test_py3kwarn.TestStdlibRemovals into a context manager, namely test_support.CleanImport. ........ r63039 | georg.brandl | 2008-05-11 03:06:05 -0400 (Sun, 11 May 2008) | 2 lines #2742: ``''`` is not converted to NULL in getaddrinfo. ........ r63040 | alexandre.vassalotti | 2008-05-11 03:08:12 -0400 (Sun, 11 May 2008) | 2 lines Fixed typo in a comment of test_support.CleanImport. ........ r63041 | alexandre.vassalotti | 2008-05-11 03:10:25 -0400 (Sun, 11 May 2008) | 2 lines Removed a dead line of code. ........ r63043 | georg.brandl | 2008-05-11 04:47:53 -0400 (Sun, 11 May 2008) | 2 lines #2812: document property.getter/setter/deleter. ........ r63049 | georg.brandl | 2008-05-11 05:06:30 -0400 (Sun, 11 May 2008) | 2 lines #1153769: document PEP 237 changes to string formatting. ........ r63050 | georg.brandl | 2008-05-11 05:11:40 -0400 (Sun, 11 May 2008) | 2 lines #2809: elaborate str.split docstring a bit. ........ r63051 | georg.brandl | 2008-05-11 06:13:59 -0400 (Sun, 11 May 2008) | 2 lines Fix typo. ........ r63052 | georg.brandl | 2008-05-11 06:33:27 -0400 (Sun, 11 May 2008) | 2 lines #2709: clarification. ........ r63053 | georg.brandl | 2008-05-11 06:42:28 -0400 (Sun, 11 May 2008) | 2 lines #2659: add ``break_on_hyphens`` to TextWrapper. ........ r63057 | georg.brandl | 2008-05-11 06:59:39 -0400 (Sun, 11 May 2008) | 2 lines #2741: clarification of value range for address_family. ........ r63058 | georg.brandl | 2008-05-11 07:09:35 -0400 (Sun, 11 May 2008) | 2 lines #2452: timeout is used for all blocking operations. ........ r63059 | andrew.kuchling | 2008-05-11 09:33:56 -0400 (Sun, 11 May 2008) | 2 lines #1792: Improve performance of marshal.dumps() on large objects by increasing the size of the buffer more quickly. ........ r63060 | andrew.kuchling | 2008-05-11 10:00:00 -0400 (Sun, 11 May 2008) | 1 line #1858: re-apply patch for this, adding the missing files ........ r63061 | benjamin.peterson | 2008-05-11 10:13:25 -0400 (Sun, 11 May 2008) | 2 lines Add the "until" command to pdb ........ r63062 | georg.brandl | 2008-05-11 10:17:13 -0400 (Sun, 11 May 2008) | 2 lines Add some sentence endings. ........
2008-05-15 21:03:33 -03:00
method call ``connect(host)`` is made. When *user* is given, additionally
the method call ``login(user, passwd, acct)`` is made (where *passwd* and
*acct* default to the empty string when not given). The optional *timeout*
parameter specifies a timeout in seconds for blocking operations like the
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
2008-06-10 14:40:04 -03:00
connection attempt (if is not specified, the global default timeout setting
will be used). *source_address* is a 2-tuple ``(host, port)`` for the socket
to bind to as its source address before connecting. The *encoding* parameter
specifies the encoding for directories and filenames.
2007-08-15 11:28:22 -03:00
The :class:`FTP` class supports the :keyword:`with` statement, e.g.:
>>> from ftplib import FTP
>>> with FTP("ftp1.at.proftpd.org") as ftp:
... ftp.login()
... ftp.dir()
... # doctest: +SKIP
'230 Anonymous login ok, restrictions apply.'
dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .
dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 ..
dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS
dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora
>>>
.. versionchanged:: 3.2
Support for the :keyword:`with` statement was added.
.. versionchanged:: 3.3
*source_address* parameter was added.
.. versionchanged:: 3.9
If the *timeout* parameter is set to be zero, it will raise a
:class:`ValueError` to prevent the creation of a non-blocking socket.
The *encoding* parameter was added, and the default was changed from
Latin-1 to UTF-8 to follow :rfc:`2640`.
Several :class:`!FTP` methods are available in two flavors:
one for handling text files and another for binary files.
The methods are named for the command which is used followed by
``lines`` for the text version or ``binary`` for the binary version.
:class:`FTP` instances have the following methods:
.. method:: FTP.set_debuglevel(level)
Set the instance's debugging level. This controls the amount of debugging
output printed. The default, ``0``, produces no debugging output. A value of
``1`` produces a moderate amount of debugging output, generally a single line
per request. A value of ``2`` or higher produces the maximum amount of
debugging output, logging each line sent and received on the control connection.
.. method:: FTP.connect(host='', port=0, timeout=None, source_address=None)
Connect to the given host and port. The default port number is ``21``, as
specified by the FTP protocol specification. It is rarely needed to specify a
different port number. This function should be called only once for each
instance; it should not be called at all if a host was given when the instance
was created. All other methods can only be used after a connection has been
made.
The optional *timeout* parameter specifies a timeout in seconds for the
connection attempt. If no *timeout* is passed, the global default timeout
setting will be used.
*source_address* is a 2-tuple ``(host, port)`` for the socket to bind to as
its source address before connecting.
.. audit-event:: ftplib.connect self,host,port ftplib.FTP.connect
.. versionchanged:: 3.3
*source_address* parameter was added.
.. method:: FTP.getwelcome()
2007-08-15 11:28:22 -03:00
Return the welcome message sent by the server in reply to the initial
connection. (This message sometimes contains disclaimers or help information
that may be relevant to the user.)
2007-08-15 11:28:22 -03:00
.. method:: FTP.login(user='anonymous', passwd='', acct='')
2007-08-15 11:28:22 -03:00
Log in as the given *user*. The *passwd* and *acct* parameters are optional and
default to the empty string. If no *user* is specified, it defaults to
``'anonymous'``. If *user* is ``'anonymous'``, the default *passwd* is
``'anonymous@'``. This function should be called only once for each instance,
after a connection has been established; it should not be called at all if a
host and user were given when the instance was created. Most FTP commands are
only allowed after the client has logged in. The *acct* parameter supplies
"accounting information"; few systems implement this.
2007-08-15 11:28:22 -03:00
.. method:: FTP.abort()
2007-08-15 11:28:22 -03:00
Abort a file transfer that is in progress. Using this does not always work, but
it's worth a try.
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done partially before. Also add a comment describing how this might have to work with different versions of the interpreter. ........ r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line Fixed typo ........ r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........ r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line Fix internal reference. ........ r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines data descriptors do not override the class dictionary if __get__ is not defined Adjust documentation and add a test to verify this behavior. See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for discussion. ........ r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines Do not compile stubs for the sha2 series hashes in the openssl hashlib module when the openssl version is too old to support them. That leads both compiled code bloat and to unittests attempting to test implementations that don't exist for comparison purposes on such platforms. ........ r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line Add two more test_strtod test values. ........ r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line #7762: fix refcount annotation of PyUnicode_Tailmatch(). ........ r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line #7725: fix referencing issue. ........ r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line mention from_float() in error message ........ r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line #7802: fix invalid example (heh). ........ r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines Fix-up ftplib documentation: move exception descriptions to toplevel, not inside a class remove attribution in "versionadded" spell and grammar check docstring of FTP_TLS ........ r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line Minor modification to unittest documentation. ........
2010-02-02 22:35:45 -04:00
.. method:: FTP.sendcmd(cmd)
2007-08-15 11:28:22 -03:00
Send a simple command string to the server and return the response string.
2007-08-15 11:28:22 -03:00
.. audit-event:: ftplib.sendcmd self,cmd ftplib.FTP.sendcmd
2007-08-15 11:28:22 -03:00
.. method:: FTP.voidcmd(cmd)
2007-08-15 11:28:22 -03:00
Send a simple command string to the server and handle the response. Return
nothing if a response code corresponding to success (codes in the range
200--299) is received. Raise :exc:`error_reply` otherwise.
2007-08-15 11:28:22 -03:00
.. audit-event:: ftplib.sendcmd self,cmd ftplib.FTP.voidcmd
2007-08-15 11:28:22 -03:00
.. method:: FTP.retrbinary(cmd, callback, blocksize=8192, rest=None)
2007-08-15 11:28:22 -03:00
Retrieve a file in binary transfer mode. *cmd* should be an appropriate
``RETR`` command: ``'RETR filename'``. The *callback* function is called for
each block of data received, with a single bytes argument giving the data
block. The optional *blocksize* argument specifies the maximum chunk size to
read on the low-level socket object created to do the actual transfer (which
will also be the largest size of the data blocks passed to *callback*). A
reasonable default is chosen. *rest* means the same thing as in the
:meth:`transfercmd` method.
2007-08-15 11:28:22 -03:00
.. method:: FTP.retrlines(cmd, callback=None)
2007-08-15 11:28:22 -03:00
Retrieve a file or directory listing in the encoding specified by the
*encoding* parameter at initialization.
*cmd* should be an appropriate ``RETR`` command (see :meth:`retrbinary`) or
a command such as ``LIST`` or ``NLST`` (usually just the string ``'LIST'``).
``LIST`` retrieves a list of files and information about those files.
``NLST`` retrieves a list of file names.
The *callback* function is called for each line with a string argument
containing the line with the trailing CRLF stripped. The default *callback*
prints the line to ``sys.stdout``.
2007-08-15 11:28:22 -03:00
.. method:: FTP.set_pasv(val)
2007-08-15 11:28:22 -03:00
Enable "passive" mode if *val* is true, otherwise disable passive mode.
Passive mode is on by default.
.. method:: FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)
2007-08-15 11:28:22 -03:00
Store a file in binary transfer mode. *cmd* should be an appropriate
``STOR`` command: ``"STOR filename"``. *fp* is a :term:`file object`
(opened in binary mode) which is read until EOF using its :meth:`~io.IOBase.read`
method in blocks of size *blocksize* to provide the data to be stored.
The *blocksize* argument defaults to 8192. *callback* is an optional single
parameter callable that is called on each block of data after it is sent.
*rest* means the same thing as in the :meth:`transfercmd` method.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.2
*rest* parameter added.
2007-08-15 11:28:22 -03:00
.. method:: FTP.storlines(cmd, fp, callback=None)
2007-08-15 11:28:22 -03:00
Store a file in line mode. *cmd* should be an appropriate
``STOR`` command (see :meth:`storbinary`). Lines are read until EOF from the
:term:`file object` *fp* (opened in binary mode) using its :meth:`~io.IOBase.readline`
method to provide the data to be stored. *callback* is an optional single
parameter callable that is called on each line after it is sent.
2007-08-15 11:28:22 -03:00
.. method:: FTP.transfercmd(cmd, rest=None)
2007-08-15 11:28:22 -03:00
Initiate a transfer over the data connection. If the transfer is active, send an
``EPRT`` or ``PORT`` command and the transfer command specified by *cmd*, and
accept the connection. If the server is passive, send an ``EPSV`` or ``PASV``
command, connect to it, and start the transfer command. Either way, return the
socket for the connection.
2007-08-15 11:28:22 -03:00
If optional *rest* is given, a ``REST`` command is sent to the server, passing
*rest* as an argument. *rest* 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 the :meth:`transfercmd`
method converts *rest* to a string with the *encoding* parameter specified
at initialization, but no check is performed on the string's contents. If the
server does not recognize the ``REST`` command, an :exc:`error_reply` exception
will be raised. If this happens, simply call :meth:`transfercmd` without a
*rest* argument.
2007-08-15 11:28:22 -03:00
.. method:: FTP.ntransfercmd(cmd, rest=None)
2007-08-15 11:28:22 -03:00
Like :meth:`transfercmd`, but returns a tuple of the data connection and the
expected size of the data. If the expected size could not be computed, ``None``
will be returned as the expected size. *cmd* and *rest* means the same thing as
in :meth:`transfercmd`.
2007-08-15 11:28:22 -03:00
.. method:: FTP.mlsd(path="", facts=[])
2007-08-15 11:28:22 -03:00
List a directory in a standardized format by using ``MLSD`` command
(:rfc:`3659`). If *path* is omitted the current directory is assumed.
*facts* is a list of strings representing the type of information desired
(e.g. ``["type", "size", "perm"]``). Return a generator object yielding a
tuple of two elements for every file found in path. First element is the
file name, the second one is a dictionary containing facts about the file
name. Content of this dictionary might be limited by the *facts* argument
but server is not guaranteed to return all requested facts.
2007-08-15 11:28:22 -03:00
.. versionadded:: 3.3
2007-08-15 11:28:22 -03:00
.. method:: FTP.nlst(argument[, ...])
2007-08-15 11:28:22 -03:00
Return a list of file names as returned by the ``NLST`` command. The
optional *argument* is a directory to list (default is the current server
directory). Multiple arguments can be used to pass non-standard options to
the ``NLST`` command.
2007-08-15 11:28:22 -03:00
.. note:: If your server supports the command, :meth:`mlsd` offers a better API.
2007-08-15 11:28:22 -03:00
.. method:: FTP.dir(argument[, ...])
2007-08-15 11:28:22 -03:00
Produce a directory listing as returned by the ``LIST`` command, printing it to
standard output. The optional *argument* is a directory to list (default is the
current server directory). Multiple arguments can be used to pass non-standard
options to the ``LIST`` command. If the last argument is a function, it is used
as a *callback* function as for :meth:`retrlines`; the default prints to
``sys.stdout``. This method returns ``None``.
2007-08-15 11:28:22 -03:00
.. note:: If your server supports the command, :meth:`mlsd` offers a better API.
2007-08-15 11:28:22 -03:00
.. method:: FTP.rename(fromname, toname)
2007-08-15 11:28:22 -03:00
Rename file *fromname* on the server to *toname*.
2007-08-15 11:28:22 -03:00
.. method:: FTP.delete(filename)
Remove the file named *filename* from the server. If successful, returns the
text of the response, otherwise raises :exc:`error_perm` on permission errors or
:exc:`error_reply` on other errors.
2007-08-15 11:28:22 -03:00
.. method:: FTP.cwd(pathname)
2007-08-15 11:28:22 -03:00
Set the current directory on the server.
2007-08-15 11:28:22 -03:00
.. method:: FTP.mkd(pathname)
2007-08-15 11:28:22 -03:00
Create a new directory on the server.
2007-08-15 11:28:22 -03:00
.. method:: FTP.pwd()
2007-08-15 11:28:22 -03:00
Return the pathname of the current directory on the server.
2007-08-15 11:28:22 -03:00
.. method:: FTP.rmd(dirname)
2007-08-15 11:28:22 -03:00
Remove the directory named *dirname* on the server.
.. method:: FTP.size(filename)
Request the size of the file named *filename* on the server. On success, the
size of the file is returned as an integer, otherwise ``None`` is returned.
Note that the ``SIZE`` command is not standardized, but is supported by many
common server implementations.
2007-08-15 11:28:22 -03:00
.. method:: FTP.quit()
2007-08-15 11:28:22 -03:00
Send a ``QUIT`` command to the server and close the connection. This is the
"polite" way to close a connection, but it may raise an exception if the server
responds with an error to the ``QUIT`` command. This implies a call to the
:meth:`close` method which renders the :class:`FTP` instance useless for
subsequent calls (see below).
2007-08-15 11:28:22 -03:00
.. method:: FTP.close()
2007-08-15 11:28:22 -03:00
Close the connection unilaterally. This should not be applied to an already
closed connection such as after a successful call to :meth:`~FTP.quit`.
After this call the :class:`FTP` instance should not be used any more (after
a call to :meth:`close` or :meth:`~FTP.quit` you cannot reopen the
connection by issuing another :meth:`login` method).
2007-08-15 11:28:22 -03:00
FTP_TLS objects
^^^^^^^^^^^^^^^
2007-08-15 11:28:22 -03:00
.. class:: FTP_TLS(host='', user='', passwd='', acct='', *, context=None,
timeout=None, source_address=None, encoding='utf-8')
2007-08-15 11:28:22 -03:00
A :class:`FTP` subclass which adds TLS support to FTP as described in
:rfc:`4217`.
Connect as usual to port 21 implicitly securing the FTP control connection
before authenticating. Securing the data connection requires the user to
explicitly ask for it by calling the :meth:`prot_p` method. *context*
is a :class:`ssl.SSLContext` object which allows bundling SSL configuration
options, certificates and private keys into a single (potentially
long-lived) structure. Please read :ref:`ssl-security` for best practices.
2007-08-15 11:28:22 -03:00
.. versionadded:: 3.2
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.3
*source_address* parameter was added.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.4
The class now supports hostname check with
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:const:`ssl.HAS_SNI`).
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.9
If the *timeout* parameter is set to be zero, it will raise a
:class:`ValueError` to prevent the creation of a non-blocking socket.
The *encoding* parameter was added, and the default was changed from
Latin-1 to UTF-8 to follow :rfc:`2640`.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.12
The deprecated *keyfile* and *certfile* parameters have been removed.
2007-08-15 11:28:22 -03:00
Here's a sample session using the :class:`FTP_TLS` class::
2007-08-15 11:28:22 -03:00
>>> ftps = FTP_TLS('ftp.pureftpd.org')
>>> ftps.login()
'230 Anonymous user logged in'
>>> ftps.prot_p()
'200 Data protection level set to "private"'
>>> ftps.nlst()
['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']
2007-08-15 11:28:22 -03:00
:class:`!FTP_TLS` class inherits from :class:`FTP`,
defining these additional methods and attributes:
2007-08-15 11:28:22 -03:00
.. attribute:: FTP_TLS.ssl_version
2007-08-15 11:28:22 -03:00
The SSL version to use (defaults to :data:`ssl.PROTOCOL_SSLv23`).
2007-08-15 11:28:22 -03:00
.. method:: FTP_TLS.auth()
2007-08-15 11:28:22 -03:00
Set up a secure control connection by using TLS or SSL, depending on what
is specified in the :attr:`ssl_version` attribute.
2007-08-15 11:28:22 -03:00
.. versionchanged:: 3.4
The method now supports hostname check with
:attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
:const:`ssl.HAS_SNI`).
2007-08-15 11:28:22 -03:00
.. method:: FTP_TLS.ccc()
2007-08-15 11:28:22 -03:00
Revert control channel back to plaintext. This can be useful to take
advantage of firewalls that know how to handle NAT with non-secure FTP
without opening fixed ports.
2007-08-15 11:28:22 -03:00
.. versionadded:: 3.3
2007-08-15 11:28:22 -03:00
.. method:: FTP_TLS.prot_p()
2007-08-15 11:28:22 -03:00
Set up secure data connection.
2007-08-15 11:28:22 -03:00
.. method:: FTP_TLS.prot_c()
2007-08-15 11:28:22 -03:00
Set up clear text data connection.
2007-08-15 11:28:22 -03:00
Module variables
^^^^^^^^^^^^^^^^
2007-08-15 11:28:22 -03:00
.. exception:: error_reply
2007-08-15 11:28:22 -03:00
Exception raised when an unexpected reply is received from the server.
2007-08-15 11:28:22 -03:00
.. exception:: error_temp
Exception raised when an error code signifying a temporary error (response
codes in the range 400--499) is received.
.. exception:: error_perm
Exception raised when an error code signifying a permanent error (response
codes in the range 500--599) is received.
.. exception:: error_proto
Exception raised when a reply is received from the server that does not fit
the response specifications of the File Transfer Protocol, i.e. begin with a
digit in the range 1--5.
.. data:: all_errors
The set of all exceptions (as a tuple) that methods of :class:`FTP`
instances may raise as a result of problems with the FTP connection (as
opposed to programming errors made by the caller). This set includes the
four exceptions listed above as well as :exc:`OSError` and :exc:`EOFError`.
.. seealso::
Module :mod:`netrc`
Parser for the :file:`.netrc` file format. The file :file:`.netrc` is
typically used by FTP clients to load user authentication information
before prompting the user.