netrc --- netrc 文件處理?

源代碼: Lib/netrc.py


netrc 類解析并封裝了 Unix 的 ftp 程序和其他 FTP 客戶端所使用的 netrc 文件格式。

class netrc.netrc([file])?

netrc 的實(shí)例或其子類的實(shí)例會(huì)被用來封裝來自 netrc 文件的數(shù)據(jù)。 如果有初始化參數(shù),它將指明要解析的文件。 如果未給出參數(shù),則位于用戶家目錄的 .netrc 文件 -- 即 os.path.expanduser() 所確定的文件 -- 將會(huì)被讀取。 在其他情況下,則將引發(fā) FileNotFoundError 異常。 解析錯(cuò)誤將引發(fā) NetrcParseError 并附帶診斷信息,包括文件名、行號(hào)以及終止令牌。 如果在 POSIX 系統(tǒng)上未指明參數(shù),則當(dāng) .netrc 文件中有密碼時(shí),如果文件歸屬或權(quán)限不安全(歸屬的用戶不是運(yùn)行進(jìn)程的用戶,或者可供任何其他用戶讀取或?qū)懭耄⒁l(fā) NetrcParseError。 這實(shí)現(xiàn)了與 ftp 和其他使用 .netrc 的程序同等的安全行為。

在 3.4 版更改: 添加了 POSIX 權(quán)限檢查。

在 3.7 版更改: 當(dāng)未將 file 作為參數(shù)傳入時(shí)會(huì)使用 os.path.expanduser() 來查找 .netrc 文件的位置。

在 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.

exception netrc.NetrcParseError?

當(dāng)在源文本中遇到語法錯(cuò)誤時(shí)由 netrc 類引發(fā)的異常。 此異常的實(shí)例提供了三個(gè)有用屬性: msg 為錯(cuò)誤的文本說明,filename 為源文件的名稱,而 lineno 給出了錯(cuò)誤所在的行號(hào)。

netrc 對(duì)象?

netrc 實(shí)例具有下列方法:

netrc.authenticators(host)?

針對(duì) host 的身份驗(yàn)證者返回一個(gè) 3 元組 (login, account, password)。 如果 netrc 文件不包含針對(duì)給定主機(jī)的條目,則返回關(guān)聯(lián)到 'default' 條目的元組。 如果匹配的主機(jī)或默認(rèn)條目均不可用,則返回 None。

netrc.__repr__()?

將類數(shù)據(jù)以 netrc 文件的格式轉(zhuǎn)儲(chǔ)為一個(gè)字符串。 (這會(huì)丟棄注釋并可能重排條目順序。)

netrc 的實(shí)例具有一些公共實(shí)例變量:

netrc.hosts?

將主機(jī)名映射到 (login, account, password) 元組的字典。 如果存在 'default' 條目,則會(huì)表示為使用該名稱的偽主機(jī)。

netrc.macros?

將宏名稱映射到字符串列表的字典。