netrc — netrc 文件处理

源代码: Lib/netrc.py


netrc 类剖析和封装的 netrc 文件格式用于 Unix ftp 程序和其它 FTP (文件传输协议) 客户端。

class netrc. netrc ( [ file ] )

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 未作为自变量传递。

exception netrc. NetrcParseError

异常被引发通过 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.

netrc 对象

netrc 实例具有下列方法:

netrc. authenticators ( host )

返回 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 .

netrc. __repr__ ( )

Dump the class data as a string in the format of a netrc file. (This discards comments and may reorder the entries.)

实例化的 netrc 拥有公共实例变量:

netrc. hosts

字典映射主机名到 (login, account, password) tuples. The ‘default’ entry, if any, is represented as a pseudo-host by that name.

netrc. macros

将宏名称映射到字符串列表的字典。

注意

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.

内容表

上一话题

configparser — 配置文件剖析器

下一话题

xdrlib — 编码和解码 XDR 数据

本页