Esempio n. 1
0
 def _getHttpSession(self):
     '''
         This is a lazy initializer for the HTTP session.
         It does not need to be active until it is required.
     '''
     if self.httpSession is None:
         self.httpSession = requests.Session()
         if not self.httpRedirect:
             from requests.adapters import HTTPAdapter
             try:
                 from requests.packages.urllib3.poolmanager import PoolManager
             except ImportError:
                 try:
                     # This import is for older versions of requests
                     from urllib3.poolmanager import PoolManager
                 except Exception:
                     raise Exception(
                         'Failed to import PoolManager from the requests module'
                     )
             import ssl
             httpAdapter = HTTPAdapter()
             httpAdapter.poolmanager = PoolManager(
                 ssl_version=ssl.PROTOCOL_TLSv1)
             self.httpSession.mount('https://', httpAdapter)
     return self.httpSession
Esempio n. 2
0
 def _get_http_session(self):
     '''
         This is a lazy initializer for the HTTP session.
         It does not need to be active until it is required.
     '''
     if self.http_session is None:
         self.http_session = requests.Session()
         if not self.http_redirect:
             from requests.adapters import HTTPAdapter
             from requests.packages.urllib3.poolmanager import PoolManager
             import ssl
             http_adapter = HTTPAdapter()
             http_adapter.poolmanager = PoolManager(
                 ssl_version=ssl.PROTOCOL_TLSv1)
             self.http_session.mount('https://', http_adapter)
     return self.http_session
def download_result_file(url, result_file_directory, result_file_name,
                         decompress, overwrite):
    """ Download file with specified URL and download parameters.

    :param result_file_directory: The download result local directory name.
    :type result_file_directory: str
    :param result_file_name: The download result local file name.
    :type result_file_name: str
    :param decompress: Determines whether to decompress the ZIP file.
                        If set to true, the file will be decompressed after download.
                        The default value is false, in which case the downloaded file is not decompressed.
    :type decompress: bool
    :param overwrite: Indicates whether the result file should overwrite the existing file if any.
    :type overwrite: bool
    :return: The download file path.
    :rtype: str
    """

    if result_file_directory is None:
        raise ValueError('result_file_directory cannot be None.')

    if result_file_name is None:
        result_file_name = "default_file_name"

    if decompress:
        name, ext = os.path.splitext(result_file_name)
        if ext == '.zip':
            raise ValueError(
                "Result file can't be decompressed into a file with extension 'zip'."
                " Please change the extension of the result_file_name or pass decompress=false"
            )
        zip_file_path = os.path.join(result_file_directory, name + '.zip')
        result_file_path = os.path.join(result_file_directory,
                                        result_file_name)
    else:
        result_file_path = os.path.join(result_file_directory,
                                        result_file_name)
        zip_file_path = result_file_path

    if os.path.exists(result_file_path) and overwrite is False:
        if six.PY3:
            raise FileExistsError(
                'Result file: {0} exists'.format(result_file_path))
        else:
            raise OSError('Result file: {0} exists'.format(result_file_path))

    pool_manager = PoolManager(ssl_version=ssl.PROTOCOL_SSLv3, )
    http_adapter = HTTPAdapter()
    http_adapter.poolmanager = pool_manager

    s = requests.Session()
    s.mount('https://', http_adapter)
    r = s.get(url, stream=True, verify=True)
    r.raise_for_status()
    try:
        with open(zip_file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=4096):
                if chunk:
                    f.write(chunk)
                    f.flush()
        if decompress:
            with contextlib.closing(
                    zipfile.ZipFile(zip_file_path)) as compressed:
                first = compressed.namelist()[0]
                with open(result_file_path, 'wb') as f:
                    f.write(compressed.read(first))
    except Exception as ex:
        raise ex
    finally:
        if decompress and os.path.exists(zip_file_path):
            os.remove(zip_file_path)
    return result_file_path
def download_result_file(url, result_file_directory, result_file_name, decompress, overwrite):
    """ Download file with specified URL and download parameters.

    :param result_file_directory: The download result local directory name.
    :type result_file_directory: str
    :param result_file_name: The download result local file name.
    :type result_file_name: str
    :param decompress: Determines whether to decompress the ZIP file.
                        If set to true, the file will be decompressed after download.
                        The default value is false, in which case the downloaded file is not decompressed.
    :type decompress: bool
    :param overwrite: Indicates whether the result file should overwrite the existing file if any.
    :type overwrite: bool
    :return: The download file path.
    :rtype: str
    """

    if result_file_directory is None:
        raise ValueError('result_file_directory cannot be None.')

    if result_file_name is None:
        result_file_name="default_file_name"

    if decompress:
        name, ext=os.path.splitext(result_file_name)
        if ext == '.zip':
            raise ValueError("Result file can't be decompressed into a file with extension 'zip'."
                                " Please change the extension of the result_file_name or pass decompress=false")
        zip_file_path=os.path.join(result_file_directory, name + '.zip')
        result_file_path=os.path.join(result_file_directory, result_file_name)
    else:
        result_file_path=os.path.join(result_file_directory, result_file_name)
        zip_file_path=result_file_path

    if os.path.exists(result_file_path) and overwrite is False:
        if six.PY3:
            raise FileExistsError('Result file: {0} exists'.format(result_file_path))
        else:
            raise OSError('Result file: {0} exists'.format(result_file_path))
    
    pool_manager=PoolManager(
        ssl_version=ssl.PROTOCOL_SSLv3,
    )
    http_adapter=HTTPAdapter()
    http_adapter.poolmanager=pool_manager
    
    s=requests.Session()
    s.mount('https://', http_adapter)
    r=s.get(url, stream=True, verify=True)
    r.raise_for_status()
    try:
        with open(zip_file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=4096):
                if chunk:
                    f.write(chunk)
                    f.flush()
        if decompress:
            with contextlib.closing(zipfile.ZipFile(zip_file_path)) as compressed:
                first=compressed.namelist()[0]
                with open(result_file_path, 'wb') as f:
                    f.write(compressed.read(first))
    except Exception as ex:
        raise ex
    finally:
        if decompress and os.path.exists(zip_file_path):
            os.remove(zip_file_path)
    return result_file_path