{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Netrc: Managing your login credentials\n", "\n", "`.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. \n", "\n", "**DataDownloader** provides a `Netrc` class to manage your `.netrc` file. The details of the class can be found [here](https://data-downloader.readthedocs.io/en/latest/api/netrc.html).\n", "\n", "Following codes show how to using the `Netrc` class to manage your `.netrc` file.\n", "\n", "## Import and create a Netrc object" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from data_downloader import downloader\n", "\n", "netrc = downloader.Netrc()\n", "netrc.hosts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add a new credential record" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urs.earthdata.nasa.gov': ('username', '', 'passwd')}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netrc.add(\"urs.earthdata.nasa.gov\", \"username\", \"passwd\")\n", "netrc.hosts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "machine urs.earthdata.nasa.gov\n", "\tlogin username\n", "\tpassword passwd\n" ] } ], "source": [ "!cat ~/.netrc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the record already exists, you need set `overwrite=True` to overwrite it." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "machine urs.earthdata.nasa.gov\n", "\tlogin username\n", "\tpassword passwd_new" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netrc.add(\"urs.earthdata.nasa.gov\", \"username\", \"passwd_new\", overwrite=True)\n", "netrc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get the host from the url\n", "\n", "If you don't know the host of the server, you can use the `get_url_host` function to get it by the url." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'gpm1.gesdisc.eosdis.nasa.gov'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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\"\n", "\n", "downloader.get_url_host(url)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "machine urs.earthdata.nasa.gov\n", "\tlogin username\n", "\tpassword passwd\n", "machine gpm1.gesdisc.eosdis.nasa.gov\n", "\tlogin username\n", "\tpassword passwd" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netrc.add(downloader.get_url_host(url), \"username\", \"passwd\")\n", "netrc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remove credential records\n", "\n", "If you want to remove a record from the `.netrc` file, you can use the `remove` method." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "machine urs.earthdata.nasa.gov\n", "\tlogin username\n", "\tpassword passwd" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netrc.remove(downloader.get_url_host(url))\n", "netrc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to remove all records from the `.netrc` file, you can use the `clear` method." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netrc.clear()\n", "netrc" ] } ], "metadata": { "kernelspec": { "display_name": "geo", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }