Lớp netrc trong Python đọc dữ liệu từ file .netrc trong thư mục home của user trên Unix. File ẩn này chứa thông tin đăng nhập. Hữu ích cho các công cụ như ftp, curl để đọc credential tự động.
Chương trình dưới đây đọc file .netrc bằng module netrc.
Ví dụ
import netrc
netrc_obj = netrc.netrc()
remoteHostName = "hostname"
authTokens = netrc_obj.authenticators(remoteHostName)
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])
macroDictionary = netrc_obj.macros
print(macroDictionary)
Kết quả:
Đầu ra
Remote Host Name:hostname User Name at remote host:xxx Account Password: XXX Password for the user name at remote host:XXXXXX