The netrc Module

The netrc module parses .netrc configuration files, as shown in Example 5-18. Such files are used to store FTP usernames and passwords in a user’s home directory (don’t forget to configure things so that the file can only be read by the user: chmod 0600 ~/.netrc, in other words).

Example 5-18. Using the netrc Module

File: netrc-example-1.py

import netrc


# default is $HOME/.netrc
info = netrc.netrc("samples/sample.netrc")

login, account, password = info.authenticators("secret.fbi")
print "login", "=>", repr(login)
print "account", "=>", repr(account)
print "password", "=>", repr(password)

login => 'mulder'
account => None
password => 'trustno1'

Get Python Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.