0

I'm trying to download weather data from this server: https://goldsmr4.sci.gsfc.nasa.gov/opendap/MERRA2/M2T1NXSLV.5.12.4/

and i would like to use the script published here: https://github.com/Open-Power-System-Data/weather_data/blob/master/opendap_download/multi_processing_download.py

There seems to be a problem with the SSL certificate of the server:

CertificateError: hostname 'goldsmr4.sci.gsfc.nasa.gov' doesn't match either of
'*.gesdisc.eosdis.nasa.gov', 'gesdisc.eosdis.nasa.gov' 

How can i disable the SSL authentification?

I guess i will have to change some parameters here:

def __create_authenticated_sesseion(self):
    s = requests.Session()
    s.headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36'}
    s.auth = (self.__username, self.__password)
    s.cookies = self.__authorize_cookies_with_urllib()

or here:

def __authorize_cookies_with_urllib(self):
   username = self.__username
   password = self.__password
   top_level_url = "https://urs.earthdata.nasa.gov"


   # create an authorization handler
   p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
   p.add_password(None, top_level_url, username, password);

   auth_handler = urllib.request.HTTPBasicAuthHandler(p)
   auth_cookie_jar = cookiejar.CookieJar()
   cookie_jar = urllib.request.HTTPCookieProcessor(auth_cookie_jar)
   opener = urllib.request.build_opener(auth_handler, cookie_jar)

   urllib.request.install_opener(opener)

or here:

def __download_and_save_file(self, url, file_path):
    r = self._authenticated_session.get(url, stream=True)
    with open(file_path, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    return r.status_code

Any help is very welcome! Thanks.

Heinz_S
  • 15
  • 7
  • 1
    Possible duplicate of [How do I disable the security certificate check in Python requests](http://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-python-requests) – Josh Lee Jan 17 '17 at 16:31
  • [Use `verify=False`](http://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-python-requests?noredirect=1&lq=1) in the request methods. – aneroid Jan 17 '17 at 22:37
  • Your question title is quite clear, however, the content of your question just unnecessarily complicated your question. Try to keep your question to the point in the future so that others can help you better. – nngeek Mar 05 '20 at 14:58

1 Answers1

0

I solved the problem combining these two answers: https://stackoverflow.com/a/33770290/7105351 to disable SSL authentification and https://stackoverflow.com/a/26236748/7105351 to get it into the opener.

now my code looks like this and is working:

def __authorize_cookies_with_urllib(self):
    username = self.__username
    password = self.__password
    top_level_url = "https://urs.earthdata.nasa.gov"

    #Create new ssl context
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    httpsHandler = urllib.request.HTTPSHandler(context = ctx)

    # create an authorization handler
    p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    p.add_password(None, top_level_url, username, password);

    auth_handler = urllib.request.HTTPBasicAuthHandler(p)
    auth_cookie_jar = cookiejar.CookieJar()
    cookie_jar = urllib.request.HTTPCookieProcessor(auth_cookie_jar)
    opener = urllib.request.build_opener(auth_handler, cookie_jar, httpsHandler)

    urllib.request.install_opener(opener)

and

def __download_and_save_file(self, url, file_path):
    r = self._authenticated_session.get(url, stream=True, verify = False)
    with open(file_path, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)
    return r.status_code
Community
  • 1
  • 1
Heinz_S
  • 15
  • 7