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
netrcinstance 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.netrcin the user’s home directory – as determined byos.path.expanduser()– will be read. Otherwise, aFileNotFoundErrorexception will be raised. Parse errors will raiseNetrcParseErrorwith 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.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.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.netrczu ermitteln, wenn file nicht als Argument übergeben wird.Geändert in Version 3.10:
netrctry UTF-8 encoding before using locale specific encoding.
-
exception
netrc.NetrcParseError¶ Exception raised by the
netrcclass when syntactical errors are encountered in source text. Instances of this exception provide three interesting attributes:msgis a textual explanation of the error,filenameis the name of the source file, andlinenogives 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, wirdNonezurü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.