netrc --- netrc ファイルの処理¶
ソースコード: Lib/netrc.py
netrc クラスは、Unix ftp プログラムや他の FTP クライアントで用いられる netrc ファイル形式を解析し、カプセル化 (encapsulate) します。
- class netrc.netrc([file])¶
netrcインスタンスまたはそのサブクラスのインスタンスは、netrc ファイルのデータをカプセル化します。 初期化時の引数を指定すると、解析するファイルを指定できます。 省略すると、os.path.expanduser()が決めるユーザーのホームディレクトリにある.netrcを読み込みます。 ファイルが見つからなければFileNotFoundErrorを送出します。 解析エラーでは、ファイル名、行番号、解析が停止したトークンなどの診断情報を含むNetrcParseErrorを送出します。If no argument is specified on a POSIX system, the presence of passwords in the
.netrcfile will raise aNetrcParseErrorif 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 supportos.getuid().バージョン 3.4 で変更: POSIX パーミッションのチェックが追加されました。
バージョン 3.7 で変更: 引数で file が渡されなかったときは、
.netrcファイルの場所を探すのにos.path.expanduser()が使われるようになりました。バージョン 3.10 で変更:
netrctry UTF-8 encoding before using locale specific encoding. The entry in the netrc file no longer needs to contain all tokens. The missing tokens' value default to an empty string. All the tokens and their values now can contain arbitrary characters, like whitespace and non-ASCII characters. If the login name is anonymous, it won't trigger the security check.
- 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¶
マクロ名を文字列のリストに対応付けている辞書です。