本文整理汇总了Python中requests.adapters.HTTPAdapter.poolmanager方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPAdapter.poolmanager方法的具体用法?Python HTTPAdapter.poolmanager怎么用?Python HTTPAdapter.poolmanager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.adapters.HTTPAdapter
的用法示例。
在下文中一共展示了HTTPAdapter.poolmanager方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_result_file
# 需要导入模块: from requests.adapters import HTTPAdapter [as 别名]
# 或者: from requests.adapters.HTTPAdapter import poolmanager [as 别名]
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