netrc — netrc file processing

Quellcode: Lib/netrc.py


Die Klasse netrc parst und kapselt das netrc-Dateiformat, das vom Unix-Programm ftp und anderen FTP-Clients verwendet wird.

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.

Geändert in Version 3.4: Die POSIX-Rechteprüfung wurde hinzugefügt.

Geändert in Version 3.7: os.path.expanduser() wird verwendet, um den Speicherort der Datei .netrc zu ermitteln, wenn file nicht als Argument übergeben wird.

Geändert in Version 3.10: netrc try UTF-8 encoding before using locale specific encoding.

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 is a textual explanation of the error, filename is the name of the source file, and lineno gives the line number on which the error was found.

netrc-Objekte

Eine Instanz von netrc hat die folgenden Methoden:

netrc.authenticators(host)

Gibt ein 3-Tupel (login, account, password) mit den Authentifizierungsdaten für host zurück. Enthält die netrc-Datei keinen Eintrag für den angegebenen Host, wird das Tupel des Eintrags „default“ zurückgegeben. Ist weder ein passender Host noch ein Standardeintrag vorhanden, wird None zurückgegeben.

netrc.__repr__()

Gibt die Daten der Klasse als Zeichenkette im Format einer netrc-Datei aus. (Dabei gehen Kommentare verloren, und die Reihenfolge der Einträge kann sich ändern.)

Instanzen von netrc haben öffentliche Instanzvariablen:

netrc.hosts

Dictionary, das Hostnamen auf Tupel der Form (login, account, password) abbildet. Der Eintrag „default“ wird, falls vorhanden, als Pseudo-Host unter diesem Namen dargestellt.

netrc.macros

Dictionary, das Makronamen auf Listen von Zeichenketten abbildet.

Bemerkung

Passwords are limited to a subset of the ASCII character set. All ASCII punctuation is allowed in passwords, however, note that whitespace and non-printable characters are not allowed in passwords. This is a limitation of the way the .netrc file is parsed and may be removed in the future.