Repository: climate Updated Branches: refs/heads/master ec3fdb4f7 -> 9bb217008
http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/ocw/data_source/esgf.py ---------------------------------------------------------------------- diff --git a/ocw/data_source/esgf.py b/ocw/data_source/esgf.py index 67c307f..0dcc2e0 100644 --- a/ocw/data_source/esgf.py +++ b/ocw/data_source/esgf.py @@ -18,7 +18,14 @@ # import os -import urllib2 +import sys +if sys.version_info[0] >= 3: + from urllib.error import HTTPError +else: + # Not Python 3 - today, it is most likely to be Python 2 + # But note that this might need an update when Python 4 + # might be around one day + from urllib2 import HTTPError from ocw.esgf.constants import DEFAULT_ESGF_SEARCH from ocw.esgf.download import download @@ -137,7 +144,7 @@ def _download_files(file_urls, username, password, download_directory='/tmp'): '''''' try: logon(username, password) - except urllib2.HTTPError: + except HTTPError: raise ValueError('esgf._download_files: Invalid login credentials') for url in file_urls: http://git-wip-us.apache.org/repos/asf/climate/blob/9bb21700/ocw/esgf/download.py ---------------------------------------------------------------------- diff --git a/ocw/esgf/download.py b/ocw/esgf/download.py index 23c107b..690915c 100644 --- a/ocw/esgf/download.py +++ b/ocw/esgf/download.py @@ -17,24 +17,36 @@ # under the License. # ''' -RCMES module to download a file from ESGF. +OCW module to download a file from ESGF. ''' -import urllib2 -import httplib +import sys +if sys.version_info[0] >= 3: + from http.client import HTTPSConnection + from urllib.request import build_opener + from urllib.request import HTTPCookieProcessor + from urllib.request import HTTPSHandler +else: + # Not Python 3 - today, it is most likely to be Python 2 + # But note that this might need an update when Python 4 + # might be around one day + from httplib import HTTPSConnection + from urllib2 import build_opener + from urllib2 import HTTPCookieProcessor + from urllib2 import HTTPSHandler from os.path import expanduser, join from ocw.esgf.constants import ESGF_CREDENTIALS -class HTTPSClientAuthHandler(urllib2.HTTPSHandler): +class HTTPSClientAuthHandler(HTTPSHandler): ''' HTTP handler that transmits an X509 certificate as part of the request ''' def __init__(self, key, cert): - urllib2.HTTPSHandler.__init__(self) + HTTPSHandler.__init__(self) self.key = key self.cert = cert @@ -42,7 +54,7 @@ class HTTPSClientAuthHandler(urllib2.HTTPSHandler): return self.do_open(self.getConnection, req) def getConnection(self, host, timeout=300): - return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert) + return HTTPSConnection(host, key_file=self.key, cert_file=self.cert) def download(url, toDirectory="/tmp"): @@ -55,8 +67,8 @@ def download(url, toDirectory="/tmp"): # setup HTTP handler certFile = expanduser(ESGF_CREDENTIALS) - opener = urllib2.build_opener(HTTPSClientAuthHandler(certFile, certFile)) - opener.add_handler(urllib2.HTTPCookieProcessor()) + opener = build_opener(HTTPSClientAuthHandler(certFile, certFile)) + opener.add_handler(HTTPCookieProcessor()) # download file localFilePath = join(toDirectory, url.split('/')[-1])