使用 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP