"ftplib" --- FTPプロトコルクライアント
**************************************

**ソースコード:** Lib/ftplib.py

======================================================================

このモジュールでは "FTP" クラスと、それに関連するいくつかの項目を定義
しています。 "FTP" クラスは、FTPプロトコルのクライアント側の機能を備え
ています。このクラスを使うとFTPのいろいろな機能の自動化、例えば他のFTP
サーバのミラーリングといったことを実行するPythonプログラムを書くことが
できます。また、 "urllib.request" モジュールもFTPを使うURLを操作するの
にこのクラスを使っています。 FTP (File Transfer Protocol)についての詳
しい情報はInternet **RFC 959** を参照して下さい。

"ftplib" モジュールを使ったサンプルを以下に示します:

   >>> from ftplib import FTP
   >>> ftp = FTP('ftp.debian.org')     # connect to host, default port
   >>> ftp.login()                     # user anonymous, passwd anonymous@
   '230 Login successful.'
   >>> ftp.cwd('debian')               # change into "debian" directory
   >>> 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)
   '226 Transfer complete.'
   >>> ftp.quit()

このモジュールは以下の項目を定義しています:

class ftplib.FTP(host='', user='', passwd='', acct='', timeout=None, source_address=None)

   Return a new instance of the "FTP" class.  When *host* is given,
   the 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 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.

   "FTP" クラスは "with" 文をサポートしています。例えば:

   >>> from ftplib import FTP
   >>> with FTP("ftp1.at.proftpd.org") as ftp:
   ...     ftp.login()
   ...     ftp.dir()
   ... 
   '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
   >>>

   バージョン 3.2 で変更: "with" 構文のサポートが追加されました。

   バージョン 3.3 で変更: *source_address* 引数が追加されました。

class ftplib.FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None)

   **RFC 4217** に記述されている TLS サポートを FTP に加えた "FTP" の
   サブクラスです。認証の前に FTP コントロール接続を暗黙にセキュアにし
   、通常通りに port 21 に接続します。データ接続をセキュアにするには、
   ユーザが "prot_p()" メソッドを呼び出してそれを明示的に要求しなけれ
   ばなりません。 *context* は SSL 設定オプション、証明書、秘密鍵を一
   つの(潜在的に長生きの)構造にまとめた "ssl.SSLContext" オブジェクト
   です。ベストプラクティスについての セキュリティで考慮すべき点 をお
   読みください。

   *keyfile* と *certfile* は *context* のレガシー版です -- これらは、
   SSL 接続のための、 PEM フォーマットの秘密鍵と証明書チェーンファイル
   名(前者が *keyfile* 、後者が *certfile* )を含むことができます。

   バージョン 3.2 で追加.

   バージョン 3.3 で変更: *source_address* 引数が追加されました。

   バージョン 3.4 で変更: このクラスは "ssl.SSLContext.check_hostname"
   と *Server Name Indication* でホスト名のチェックをサポートしました
   。("ssl.HAS_SNI" を参照してください)。

   バージョン 3.6 で非推奨: *keyfile* および *certfile* は非推奨となっ
   たので、 *context* を使ってください。 代わりに
   "ssl.SSLContext.load_cert_chain()" を使うか、または
   "ssl.create_default_context()" にシステムが信頼する CA 証明書を選ん
   でもらうかしてください。

   "FTP_TLS" クラスを使ったサンプルセッションはこちらです:

      >>> 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']

exception ftplib.error_reply

   サーバから想定外の応答があったときに送出される例外。

exception ftplib.error_temp

   一時的エラーを表すエラーコード(400--499の範囲の応答コード)を受け取
   った時に発生する例外。

exception ftplib.error_perm

   永久エラーを表すエラーコード(500--599の範囲の応答コード)を受け取っ
   た時に発生する例外。

exception ftplib.error_proto

   File Transfer Protocol の応答仕様に適合しない、すなわち1--5の数字で
   始まらない応答コードをサーバから受け取った時に発生する例外。

ftplib.all_errors

   "FTP" インスタンスのメソッド実行時、FTP接続で (プログラミングのエラ
   ーと考えられるメソッドの実行によって) 発生する全ての例外 (タプル形
   式)。この例外には以上の４つのエラーはもちろん、 "OSError" と
   "EOFError" も含まれます。

参考:

  "netrc" モジュール
     ".netrc" ファイルフォーマットのパーザ。 ".netrc" ファイルは、 FTP
     クライアントがユーザにプロンプトを出す前に、ユーザ認証情報をロー
     ドするのによく使われます。


FTP オブジェクト
================

いくつかのコマンドは２つのタイプについて実行します：１つはテキストファ
イルで、もう１つはバイナリファイルを扱います。これらのメソッドのテキス
トバージョンでは "lines" 、バイナリバージョンでは "binary" の語がメソ
ッド名の終わりについています。

"FTP" インスタンスには以下のメソッドがあります:

FTP.set_debuglevel(level)

   インスタンスのデバッグレベルを設定します。この設定によってデバッグ
   時に出力される量を調節します。デフォルトは "0" で、何も出力されませ
   ん。 "1" なら、一般的に１つのコマンドあたり１行の適当な量のデバッグ
   出力を行います。 "2" 以上なら、コントロール接続で受信した各行を出力
   して、最大のデバッグ出力をします。

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.

   引数 "self", "host", "port" を指定して 監査イベント
   "ftplib.connect" を送出します。

   バージョン 3.3 で変更: *source_address* 引数が追加されました。

FTP.getwelcome()

   サーバに最初に接続した際に送信される応答中のウェルカムメッセージを
   返します。(このメッセージには時に、ユーザにとって重要な免責事項や
   ヘルプ情報が入っています。)

FTP.login(user='anonymous', passwd='', acct='')

   与えられた *user* でログインします。 *passwd* と *acct* のパラメー
   タは省略可能で、デフォルトでは空文字列です。もし *user* が指定され
   ないなら、デフォルトで "'anonymous'" になります。もし *user* が
   "'anonymous'" なら、デフォルトの *passwd* は "'anonymous@'" になり
   ます。この関数は各インスタンスについて一度だけ、接続が確立した後に
   呼び出さなければなりません。インスタンスが作られた時にホスト名とユ
   ーザ名が与えられていたら、このメソッドを実行すべきではありません。
   ほとんどのFTPコマンドはクライアントがログインした後に実行可能になり
   ます。 *acct* 引数は "accounting information" を提供します。ほとん
   どのシステムはこれを実装していません。

FTP.abort()

   実行中のファイル転送を中止します。これはいつも機能するわけではあり
   ませんが、やってみる価値はあります。

FTP.sendcmd(cmd)

   シンプルなコマンド文字列をサーバに送信して、受信した文字列を返しま
   す。

   引数 "self", "cmd" を指定して 監査イベント "ftplib.sendcmd" を送出
   します。

FTP.voidcmd(cmd)

   シンプルなコマンド文字列をサーバに送信して、その応答を扱います。応
   答コードが成功に関係するもの(200--299の範囲にあるコード)なら何も返
   しません。それ以外は "error_reply" を発生します。

   引数 "self", "cmd" を指定して 監査イベント "ftplib.sendcmd" を送出
   します。

FTP.retrbinary(cmd, callback, blocksize=8192, rest=None)

   バイナリ転送モードでファイルを受信します。 *cmd* は適切な "RETR" コ
   マンド:  "'RETR filename'" でなければなりません。関数 *callback* は
   、受信したデータブロックのそれぞれに対して、データブロックを１つの
   bytes の引数として呼び出されます。省略可能な引数 *blocksize* は、実
   際の転送を行うのに作られた低レベルのソケットオブジェクトから読み込
   む最大のチャンクサイズを指定します（これは *callback* に与えられる
   データブロックの最大サイズにもなります）。妥当なデフォルト値が設定
   されます。 *rest* は、 "transfercmd()" メソッドと同じものです。

FTP.retrlines(cmd, callback=None)

   Retrieve a file or directory listing in ASCII transfer mode.  *cmd*
   should be an appropriate "RETR" command (see "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".

FTP.set_pasv(val)

   *val* が真の場合 "パッシブ" モードを有効化し、偽の場合は無効化しま
   す。 デフォルトではパッシブモードです。

FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)

   バイナリ転送モードでファイルを転送します。 *cmd* は適切な "STOR" コ
   マンド： ""STOR filename"" でなければなりません。 *fp* は (バイナリ
   モードで開かれた) *ファイルオブジェクト* で、 "read()" メソッドで
   EOFまで読み込まれ、ブロックサイズ *blocksize* でデータが転送されま
   す。引数 *blocksize* のデフォルト値は8192です。 *callback* はオプシ
   ョンの引数で、引数を1つとる呼び出し可能オブジェクトを渡します。各デ
   ータブロックが送信された後に、そのブロックを引数にして呼び出されま
   す。 *rest* は、 "transfercmd()" メソッドにあるものと同じ意味です。

   バージョン 3.2 で変更: *rest* パラメタが追加されました。

FTP.storlines(cmd, fp, callback=None)

   Store a file in ASCII transfer mode.  *cmd* should be an
   appropriate "STOR" command (see "storbinary()").  Lines are read
   until EOF from the *file object* *fp* (opened in binary mode) using
   its "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.

FTP.transfercmd(cmd, rest=None)

   データ接続中に転送を初期化します。もし転送中なら、"EPRT" あるいは
   "PORT" コマンドと、*cmd* で指定したコマンドを送信し、接続を続けます
   。サーバがパッシブなら、"EPSV" あるいは "PASV" コマンドを送信して接
   続し、転送コマンドを開始します。どちらの場合も、接続のためのソケッ
   トを返します。

   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 **RFC 959** requires only that
   *rest* be a string containing characters in the printable range
   from ASCII code 33 to ASCII code 126.  The "transfercmd()" method,
   therefore, converts *rest* to a string, but no check is performed
   on the string's contents.  If the server does not recognize the
   "REST" command, an "error_reply" exception will be raised.  If this
   happens, simply call "transfercmd()" without a *rest* argument.

FTP.ntransfercmd(cmd, rest=None)

   "transfercmd()" と同様ですが、データと予想されるサイズとのタプルを
   返します。もしサイズが計算できないなら、サイズの代わりに "None" が
   返されます。 *cmd* と *rest* は "transfercmd()" のものと同じです。

FTP.mlsd(path="", facts=[])

   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.

   バージョン 3.3 で追加.

FTP.nlst(argument[, ...])

   "NLST" コマンドで返されるファイル名のリストを返します。省略可能な
   *argument* は、リストアップするディレクトリです（デフォルトではサー
   バのカレントディレクトリです）。 "NLST" コマンドに非標準である複数
   の引数を渡すことができます。

   注釈:

     If your server supports the command, "mlsd()" offers a better
     API.

FTP.dir(argument[, ...])

   "LIST" コマンドで返されるディレクトリ内のリストを作り、標準出力へ出
   力します。省略可能な *argument* は、リストアップするディレクトリで
   す（デフォルトではサーバのカレントディレクトリです）。 "LIST" コマ
   ンドに非標準である複数の引数を渡すことができます。もし最後の引数が
   関数なら、 "retrlines()" のように *callback* として使われます; デフ
   ォルトでは "sys.stdout" に印字します。このメソッドは "None" を返し
   ます。

   注釈:

     If your server supports the command, "mlsd()" offers a better
     API.

FTP.rename(fromname, toname)

   サーバ上のファイルのファイル名 *fromname* を *toname* へ変更します
   。

FTP.delete(filename)

   サーバからファイル *filename* を削除します。成功したら応答のテキス
   トを返し、そうでないならパーミッションエラーでは "error_perm" を、
   他のエラーでは "error_reply" を返します。

FTP.cwd(pathname)

   サーバのカレントディレクトリを設定します。

FTP.mkd(pathname)

   サーバ上に新たにディレクトリを作ります。

FTP.pwd()

   サーバ上のカレントディレクトリのパスを返します。

FTP.rmd(dirname)

   サーバ上のディレクトリ *dirname* を削除します。

FTP.size(filename)

   サーバ上のファイル *filename* のサイズを尋ねます。成功したらファイ
   ルサイズが整数で返され、そうでないなら "None" が返されます。 "SIZE"
   コマンドは標準化されていませんが、多くの普通のサーバで実装されてい
   ることに注意して下さい。

FTP.quit()

   サーバに "QUIT" コマンドを送信し、接続を閉じます。これは接続を閉じ
   るのに"礼儀正しい"方法ですが、 "QUIT" コマンドに反応してサーバの例
   外が発生するかもしれません。この例外は、 "close()" メソッドによって
   "FTP" インスタンスに対するその後のコマンド使用が不可になっているこ
   とを示しています（下記参照）。

FTP.close()

   接続を一方的に閉じます。既に閉じた接続に対して実行すべきではありま
   せん（例えば "quit()" を呼び出して成功した後など）。この実行の後、
   "FTP" インスタンスはもう使用すべきではありません（ "close()" あるい
   は "quit()" を呼び出した後で、 "login()" メソッドをもう一度実行して
   再び接続を開くことはできません）。


FTP_TLS オブジェクト
====================

"FTP_TLS" クラスは "FTP" を継承し、さらにオブジェクトを定義します:

FTP_TLS.ssl_version

   使用する SSL のバージョン (デフォルトは "ssl.PROTOCOL_SSLv23") です
   。

FTP_TLS.auth()

   "ssl_version" 属性で指定されたものに従って、 TLS または SSL を使い
   、セキュアコントロール接続をセットアップします。

   バージョン 3.4 で変更: このメソッドは
   "ssl.SSLContext.check_hostname" と *Server Name Indication* でホス
   ト名のチェックをサポートしました。("ssl.HAS_SNI" を参照してください
   )。

FTP_TLS.ccc()

   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.

   バージョン 3.3 で追加.

FTP_TLS.prot_p()

   セキュアデータ接続をセットアップします。

FTP_TLS.prot_c()

   平文データ接続をセットアップします。
