netrc —- netrc file processing
Source code:Lib/netrc.py
The netrc
class parses and encapsulates the netrc file format used bythe Unix ftp program and other FTP clients.
- class
netrc.
netrc
([file]) - A
netrc
instance or subclass instance encapsulates data from a netrcfile. The initialization argument, if present, specifies the file to parse. Ifno argument is given, the file.netrc
in the user's home directory —as determined byos.path.expanduser()
— will be read. Otherwise,aFileNotFoundError
exception will be raised.Parse errors will raiseNetrcParseError
with diagnosticinformation including the file name, line number, and terminating token.If no argument is specified on a POSIX system, the presence of passwords inthe.netrc
file will raise aNetrcParseError
if 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
.
在 3.4 版更改: Added the POSIX permission check.
在 3.7 版更改: os.path.expanduser()
is used to find the location of the.netrc
file when file is not passed as argument.
- exception
netrc.
NetrcParseError
- Exception raised by the
netrc
class when syntactical errors areencountered in source text. Instances of this exception provide threeinteresting attributes:msg
is a textual explanation of the error,filename
is the name of the source file, andlineno
gives theline number on which the error was found.
netrc Objects
A netrc
instance has the following methods:
netrc.
authenticators
(host)- Return a 3-tuple
(login, account, password)
of authenticators for host.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 of netrc
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.
注解
Passwords are limited to a subset of the ASCII character set. All ASCIIpunctuation is allowed in passwords, however, note that whitespace andnon-printable characters are not allowed in passwords. This is a limitationof the way the .netrc file is parsed and may be removed in the future.