Page 1 of 1

Python 3 Earthdata downloads

Posted: Fri Aug 19, 2022 10:51 pm America/New_York
by earthengine_urs
The first example from viewtopic.php?t=3433#confirm_external_link-modal can be modified for Python 3 like this:

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, "", '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(
    # 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 = ''
    myrequest = request.Request(url)
    response = request.urlopen(myrequest)

    # Check if status is OK
    if response.code != 200:
      raise ValueError
    local_output = tempfile.NamedTemporaryFile(delete=False)
    while True:
      chunk =
      if chunk: 

Re: Python 3 Earthdata downloads

Posted: Sat Aug 20, 2022 11:56 pm America/New_York
by earthengine_urs
I think I know why some EarthData downloads stopped working. NASA download URLs get redirected to the EarthData login server which requires an authentication header. Setting this header for the original download URL used to be ok, but now it causes a 401 error, and the redirect never happens.

The fix is to only set the authentication header on the second request.