netrc
— netrc 文件处理
¶
源代码: Lib/netrc.py
netrc
类剖析和封装的 netrc 文件格式用于 Unix
ftp
程序和其它 FTP (文件传输协议) 客户端。
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
.
3.4 版改变: 添加 POSIX (便携式操作系统接口) 权限校验。
3.7 版改变:
os.path.expanduser()
用于查找位置对于
.netrc
文件当
file
未作为自变量传递。
3.10 版改变:
netrc
try 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.
异常被引发通过
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.
A
netrc
实例具有下列方法:
返回 3 元组
(login, account, password)
of authenticators for
host
. If the netrc file did not contain an entry for the given host, return the tuple associated with the ‘default’ entry. If neither matching host nor default entry is available, return
None
.
Dump the class data as a string in the format of a netrc file. (This discards comments and may reorder the entries.)
实例化的
netrc
拥有公共实例变量:
字典映射主机名到
(login, account, password)
tuples. The ‘default’ entry, if any, is represented as a pseudo-host by that name.
将宏名称映射到字符串列表的字典。