netrc — netrc file processing¶
Source code:Lib/netrc.py
Thenetrc class parses and encapsulates the netrc file format used bythe Unixftp program and other FTP clients.
- classnetrc.netrc([file])¶
A
netrcinstance or subclass instance encapsulates data from a netrcfile. The initialization argument, if present, specifies the file to parse. Ifno 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 diagnosticinformation including the file name, line number, and terminating token.If no argument is specified on a POSIX system, the presence of passwords inthe
.netrcfile will raise aNetrcParseErrorif the fileownership or permissions are insecure (owned by a user other than the userrunning the process, or accessible for read or write by any other user).This implements security behavior equivalent to that of ftp and otherprograms that use.netrc. Such security checks are not availableon platforms that do not supportos.getuid().Changed in version 3.4:Added the POSIX permission check.
Changed in version 3.7:
os.path.expanduser()is used to find the location of the.netrcfile whenfile is not passed as argument.Changed in version 3.10:
netrctry UTF-8 encoding before using locale specificencoding.The entry in the netrc file no longer needs to contain all tokens. The missingtokens’ value default to an empty string. All the tokens and their values nowcan contain arbitrary characters, like whitespace and non-ASCII characters.If the login name is anonymous, it won’t trigger the security check.
- exceptionnetrc.NetrcParseError¶
Exception raised by the
netrcclass when syntactical errors areencountered in source text. Instances of this exception provide threeinteresting 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 Objects¶
Anetrc instance has the following methods:
- netrc.authenticators(host)¶
Return a 3-tuple
(login,account,password)of authenticators forhost.If the netrc file did not contain an entry for the given host, return the tupleassociated with the ‘default’ entry. If neither matching host nor default entryis available, returnNone.
- netrc.__repr__()¶
Dump the class data as a string in the format of a netrc file. (This discardscomments and may reorder the entries.)
Instances ofnetrc have public instance variables:
- netrc.hosts¶
Dictionary mapping host names to
(login,account,password)tuples. The‘default’ entry, if any, is represented as a pseudo-host by that name.
- netrc.macros¶
Dictionary mapping macro names to string lists.