Python

Tags :: Language

General

Simple netrc example

import os
import netrc

net_fp = os.path.expanduser("~/.netrc")

assert os.path.isfile(net_fp), ".netrc does not exist"
assert oct(os.stat(net_fp).st_mode) == oct(0o100600), "incorrect permissions"

net = netrc.netrc()

# replace host with the machine name token you want to look up
creds = n.authenticators("host") # -> (username, account, password)

# will return tuple of tokens for host
creds[0] # -> username
creds[2] # -> password

Reference:


Links to this note