使用 Python 處理 netrc 檔案


python 中的 netrc 類用於讀取 Unix 系統中使用者家庭目錄中存在的 .netrc 檔案中的資料。這些是包含使用者登入憑據詳細資訊的隱藏檔案。這有助於像 ftp、curl 等工具成功讀取 .netrc 檔案並將其用於其操作。

以下程式展示瞭如何使用 python 的 netrc 模組來讀取 .netrc 檔案。

示例

import netrc
netrc = netrc.netrc()
remoteHostName = "hostname"
authTokens = netrc.authenticators(remoteHostName)
# Print the access tokens
print("Remote Host Name:%s" % (remoteHostName))
print("User Name at remote host:%s" % (authTokens[0]))
print("Account Password:%s" % (authTokens[1]))
print("Password for the user name at remote host:%s" % (authTokens[2]))
# print the macros
macroDictionary = netrc.macros
print(macroDictionary)

執行上述程式碼,結果如下:

輸出

Remote Host Name:hostname
User Name at remote host:xxx
Account Password: XXX
Password for the user name at remote host:XXXXXX

更新時間:2020 年 12 月 28 日

1 千次瀏覽

開啟 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.