netrc --- netrc 文件處理?

源代碼: Lib/netrc.py


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

class netrc.netrc([file])?

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

在 3.4 版更改: 添加了 POSIX 權限檢查。

在 3.7 版更改: 當未將 file 作為參數傳入時(shí)會(huì )使用 os.path.expanduser() 來(lái)查找 .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?

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

netrc 對象?

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

netrc.authenticators(host)?

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

netrc.__repr__()?

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

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

netrc.hosts?

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

netrc.macros?

將宏名稱(chēng)映射到字符串列表的字典。