netrc --- netrc ファイルの処理

ソースコード: Lib/netrc.py


netrc クラスは、Unix ftp プログラムや他の FTP クライアントで用いられる netrc ファイル形式を解析し、カプセル化 (encapsulate) します。

class netrc.netrc([file])

A netrc instance or subclass instance encapsulates data from a netrc file. The initialization argument, if present, specifies the file to parse. If no argument is given, the file .netrc in the user's home directory -- as determined by os.path.expanduser() -- will be read. Otherwise, a FileNotFoundError exception will be raised. Parse errors will raise NetrcParseError with diagnostic information including the file name, line number, and terminating token.

If no argument is specified on a POSIX system, the presence of passwords in the .netrc file will raise a NetrcParseError if the file ownership or permissions are insecure (owned by a user other than the user running the process, or accessible for read or write by any other user). This implements security behavior equivalent to that of ftp and other programs that use .netrc. Such security checks are not available on platforms that do not support os.getuid().

バージョン 3.4 で変更: POSIX パーミッションのチェックが追加されました。

バージョン 3.7 で変更: 引数で file が渡されなかったときは、 .netrc ファイルの場所を探すのに os.path.expanduser() が使われるようになりました。

バージョン 3.10 で変更: netrc は、ロケール固有のエンコーディングを使用する前に、 UTF-8 でのエンコーディングを試みます。 netrc ファイルのエントリがすべてのトークンを含む必要はなくなりました。欠落したトークンのデフォルト値は空文字列です。すべてのトークンとその値は空白や非 ASCII 文字のような任意の文字を使用できるようになりました。ログイン名が匿名の場合、セキュリティチェックはトリガーしません。

exception netrc.NetrcParseError

ソーステキストに構文上のエラーがある場合に、 netrc クラスで発生する例外。この例外のインスタンスは3つの興味深い属性を提供します:

msg

エラーのテキストによる説明。

filename

ソースファイルの名前。

lineno

エラーが見つかった行番号。

netrc オブジェクト

netrc インスタンスは以下のメソッドを持っています:

netrc.authenticators(host)

host の認証情報として、三要素のタプル (login, account, password) を返します。与えられた host に対するエントリが netrc ファイルにない場合、'default' エントリに関連付けられたタプルが返されます。host に対応するエントリがなく、default エントリもない場合、None を返します。

netrc.__repr__()

クラスの持っているデータを netrc ファイルの書式に従った文字列で出力します。(コメントは無視され、エントリが並べ替えられる可能性があります。)

netrc のインスタンスは以下の public なインスタンス変数を持っています:

netrc.hosts

ホスト名を (login, account, password) からなるタプルに対応づけている辞書です。'default' エントリがある場合、その名前の擬似ホスト名として表現されます。

netrc.macros

マクロ名を文字列のリストに対応付けている辞書です。