netrc — Verarbeitung von netrc-Dateien¶
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. 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¶
Ausnahme, die von der Klasse
netrcausgelöst wird, wenn im Quelltext Syntaxfehler auftreten. Instanzen dieser Ausnahme bieten drei interessante Attribute:- msg¶
Textuelle Erläuterung des Fehlers.
- filename¶
Der Name der Quelldatei.
- lineno¶
Die Zeilennummer, in der der Fehler gefunden wurde.
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.