The netrc file processing using Python

PythonServer Side ProgrammingProgramming

The netrc class in python is used to read the data from the .netrc file presnt in unix systems in user’s home firectory. These are hidden files containing user’s login credential details. This is helpful for tool slike ftp, curl etc to successfully read the ,netrc file and use it for their actions.

The below program shows how we can read the .netrc file using python’s netrc module.


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

Running the above code gives us the following result −


Remote Host Name:hostname
User Name at remote host:xxx
Account Password: XXX
Password for the user name at remote host:XXXXXX
Published on 28-Dec-2020 11:08:34