Code: Select all
import tempfile
from http import cookiejar
from urllib import request
from urllib.parse import urlencode
# Create a password manager to deal with the 401 response that is returned from
# Earthdata Login
password_manager = request.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, "https://urs.earthdata.nasa.gov", 'username', 'password')
# Create a cookie jar for storing cookies. This is used to store and return
# the session cookie given to use by the data server (otherwise it will just
# keep sending us back to Earthdata Login to authenticate). Ideally, we
# should use a file based cookie jar to preserve cookies between runs. This
# will make it much more efficient.
cookie_jar = cookiejar.CookieJar()
# Install all the handlers.
opener = request.build_opener(
request.HTTPBasicAuthHandler(password_manager),
request.HTTPCookieProcessor(cookie_jar))
request.install_opener(opener)
# Create and submit the request. There are a wide range of exceptions that
# can be thrown here, including HTTPError and URLError. These should be
# caught and handled.
url = 'https://n5eil01u.ecs.nsidc.org/MOSA/MYD10_L2.061/2002.07.04/MYD10_L2.A2002185.2040.061.2020071133128.hdf'
myrequest = request.Request(url)
response = request.urlopen(myrequest)
# Check if status is OK
if response.code != 200:
raise ValueError
response.begin()
local_output = tempfile.NamedTemporaryFile(delete=False)
while True:
chunk = response.read()
if chunk:
local_output.file.write(chunk)
else:
break
local_output.file.close()
print(local_output.name)