netrc — netrc file processing

Вихідний код: Lib/netrc.py


Клас netrc аналізує та інкапсулює формат файлу netrc, який використовується програмою Unix ftp та іншими клієнтами FTP.

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: os.path.expanduser() використовується для пошуку розташування файлу .netrc, якщо file не передається як аргумент.

Змінено в версії 3.10: netrc try 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

Exception raised by the netrc class when syntactical errors are encountered in source text. Instances of this exception provide three interesting attributes:

msg

Textual explanation of the error.

filename

The name of the source file.

lineno

The line number on which the error was found.

Об’єкти netrc

Екземпляр netrc має такі методи:

netrc.authenticators(host)

Повертає 3-кортеж (логін, обліковий запис, пароль) автентифікаторів для host. Якщо файл netrc не містить запису для даного хоста, поверніть кортеж, пов’язаний із записом «default». Якщо ні відповідний хост, ні запис за замовчуванням недоступні, поверніть None.

netrc.__repr__()

Дамп даних класу як рядок у форматі файлу netrc. (Це відхиляє коментарі та може змінити порядок записів.)

Екземпляри netrc мають публічні змінні екземпляра:

netrc.hosts

Словник зіставляє імена хостів із кортежами (логін, обліковий запис, пароль). Запис «за замовчуванням», якщо такий є, представляється як псевдохост під таким іменем.

netrc.macros

Словник зіставляє імена макросів зі списками рядків.