Netrc: Managing your login credentials#
.netrc is a file that contains login information for various servers. It’s used by programs that need to log in to servers. You can add login information to a .netrc file in your home to avoid supplying username and password each time you download data.
DataDownloader provides a Netrc class to manage your .netrc file. The details of the class can be found here.
Following codes show how to using the Netrc class to manage your .netrc file.
Import and create a Netrc object#
from data_downloader import downloader
netrc = downloader.Netrc()
netrc.hosts
{}
Add a new credential record#
netrc.add("urs.earthdata.nasa.gov", "username", "passwd")
netrc.hosts
{'urs.earthdata.nasa.gov': ('username', '', 'passwd')}
The new record is added to the .netrc file in the user’s home directory. For Linux and MacOS, the file is located at ~/.netrc. For Windows, the file is located at %HOME%\.netrc.
!cat ~/.netrc
machine urs.earthdata.nasa.gov
login username
password passwd
If the record already exists, you need set overwrite=True to overwrite it.
netrc.add("urs.earthdata.nasa.gov", "username", "passwd_new", overwrite=True)
netrc
machine urs.earthdata.nasa.gov
login username
password passwd_new
Get the host from the url#
If you don’t know the host of the server, you can use the get_url_host function to get it by the url.
url = "https://gpm1.gesdisc.eosdis.nasa.gov/daac-bin/OTF/HTTP_services.cgi?FILENAME=%2Fdata%2FGPM_L3%2FGPM_3IMERGM.06%2F2000%2F3B-MO.MS.MRG.3IMERG.20000601-S000000-E235959.06.V06B.HDF5&FORMAT=bmM0Lw&BBOX=31.904%2C99.492%2C35.771%2C105.908&LABEL=3B-MO.MS.MRG.3IMERG.20000601-S000000-E235959.06.V06B.HDF5.SUB.nc4&SHORTNAME=GPM_3IMERGM&SERVICE=L34RS_GPM&VERSION=1.02&DATASET_VERSION=06&VARIABLES=precipitation"
downloader.get_url_host(url)
'gpm1.gesdisc.eosdis.nasa.gov'
netrc.add(downloader.get_url_host(url), "username", "passwd")
netrc
machine urs.earthdata.nasa.gov
login username
password passwd
machine gpm1.gesdisc.eosdis.nasa.gov
login username
password passwd
Remove credential records#
If you want to remove a record from the .netrc file, you can use the remove method.
netrc.remove(downloader.get_url_host(url))
netrc
machine urs.earthdata.nasa.gov
login username
password passwd
If you want to remove all records from the .netrc file, you can use the clear method.
netrc.clear()
netrc