คลาส netrc ใน python ใช้เพื่ออ่านข้อมูลจากไฟล์ .netrc ที่มีอยู่ในระบบยูนิกซ์ในบ้าน firectory ของผู้ใช้ ไฟล์เหล่านี้เป็นไฟล์ที่ซ่อนอยู่ซึ่งมีรายละเอียดข้อมูลรับรองการเข้าสู่ระบบของผู้ใช้ สิ่งนี้มีประโยชน์สำหรับเครื่องมือ slike ftp, curl ฯลฯ เพื่ออ่านไฟล์ ,netrc ได้สำเร็จและใช้สำหรับการกระทำของพวกเขา
โปรแกรมด้านล่างแสดงวิธีที่เราสามารถอ่านไฟล์ .netrc โดยใช้โมดูล netrc ของ python
ตัวอย่าง
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