Improved Code
“`python
def download_7z_file(config):
“””
Download a 7z file with support for retries, timeout, SSL verification, redirects, and custom headers.
:param config: A dictionary containing download configurations. Supported keys:
- max_retries: Maximum number of retries, default is 3.
- timeout: Timeout in seconds, default is 30.
- verify_ssl: Whether to verify SSL certificates, default is True.
- allow_redirects: Whether to allow redirects, default is True.
- headers: Custom HTTP headers, default is None.
:return: Returns True if the download is successful, otherwise False.
"""
global common_config, IS_WINDOWS
# Get the list of download URLs
urls = common_config.get('urls', [])
if not urls:
print("Error: No download URLs found in the configuration.")
return False
# Get the output file path
output_file = common_config.get('output_7z_path', {}).get(IS_WINDOWS)
if not output_file:
print("Error: No valid output file path found in the configuration.")
return False
# Default configuration
default_config = {
'max_retries': 3,
'timeout': 30,
'verify_ssl': True,
'allow_redirects': True,
'headers': {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
}
# Merge user configuration with default configuration
download_config = {**default_config, **config}
for url in urls:
for attempt in range(download_config['max_retries']):
try:
# Send HTTP request
response = requests.get(
url,
headers=download_config['headers'], # Use headers from the configuration
allow_redirects=download_config['allow_redirects'],
timeout=download_config['timeout'],
stream=True,
verify=download_config['verify_ssl']
)
response.raise_for_status() # Check if the request is successful
# Get the total file size
total_size = int(response.headers.get('content-length', 0))
# Download the file and display a progress bar
with open(output_file, 'wb') as f, tqdm(
total=total_size,
unit='B',
unit_scale=True,
desc=str(output_file),
ncols=100
) as pbar:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
pbar.update(len(chunk))
print(f"File successfully downloaded to {output_file}")
return True
except requests.exceptions.SSLError as e:
print(f"SSL certificate verification failed: {e}")
if download_config['verify_ssl']:
print("You can try setting verify_ssl=False to ignore certificate verification (not recommended).")
return False
except requests.exceptions.RequestException as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt < download_config['max_retries'] - 1:
time.sleep(5) # Wait 5 seconds before retrying
else:
print("All retries failed.")
return False
return False
“`
Key Improvements
- Retrieval and Validation of
output_file
: - Use
common_config.get('output_7z_path', {}).get(IS_WINDOWS)
to retrieveoutput_file
. -
If
output_file
is empty or invalid, returnFalse
and print an error message. -
Error Messages:
-
If
output_file
is invalid, print a clear error message for easier debugging and troubleshooting. -
Global Variables:
- Ensure
common_config
andIS_WINDOWS
are global variables or pass them as function parameters.
Usage Examples
1. Normal Call
“`python
common_config = {
‘urls’: [‘https://example.com/path/to/file.7z’],
‘output_7z_path’: {
True: ‘C:/path/to/output.7z’, # IS_WINDOWS=True
False: ‘/path/to/output.7z’ # IS_WINDOWS=False
}
}
IS_WINDOWS = True
config = {
‘max_retries’: 5,
‘timeout’: 60,
‘headers’: {
‘User-Agent’: ‘My Custom User Agent’
}
}
download_7z_file(config)
“`
2. Invalid output_file
“`python
common_config = {
‘urls’: [‘https://example.com/path/to/file.7z’],
‘output_7z_path’: {} # No valid output_file path provided
}
IS_WINDOWS = True
download_7z_file({})
“`
Output:
Error: No valid output file path found in the configuration.