Use sessions in requests when downloading files
This commit is contained in:
parent
b750432381
commit
2a70997a6c
1 changed files with 14 additions and 7 deletions
|
|
@ -17,6 +17,12 @@ from wk.std import GenericError
|
||||||
# STATIC VARIABLES
|
# STATIC VARIABLES
|
||||||
ARCH = '64' if platform.architecture()[0] == '64bit' else '32'
|
ARCH = '64' if platform.architecture()[0] == '64bit' else '32'
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
HEADERS = {
|
||||||
|
'User-Agent': (
|
||||||
|
'Mozilla/5.0 (X11; Linux x86_64; rv:97.0) '
|
||||||
|
'Gecko/20100101 Firefox/97.0'
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# "GLOBAL" VARIABLES
|
# "GLOBAL" VARIABLES
|
||||||
|
|
@ -42,8 +48,9 @@ def download_file(out_path, source_url, as_new=False, overwrite=False):
|
||||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# Request download
|
# Request download
|
||||||
|
with requests.Session() as session:
|
||||||
try:
|
try:
|
||||||
response = requests.get(source_url, stream=True)
|
response = session.get(source_url, headers=HEADERS, stream=True)
|
||||||
except requests.RequestException as _err:
|
except requests.RequestException as _err:
|
||||||
download_failed = _err
|
download_failed = _err
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue