From d28bf0124193e90f654f379bc36a35a902e8356c Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 1 Jul 2019 07:28:47 -0500 Subject: [PATCH] Add option in download() to appear as browser --- openmc/_utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/openmc/_utils.py b/openmc/_utils.py index 15781282c2f..97c253f39d2 100644 --- a/openmc/_utils.py +++ b/openmc/_utils.py @@ -2,12 +2,12 @@ import os.path from pathlib import Path from urllib.parse import urlparse -from urllib.request import urlopen +from urllib.request import urlopen, Request _BLOCK_SIZE = 16384 -def download(url, checksum=None, **kwargs): +def download(url, checksum=None, as_browser=False, **kwargs): """Download file from a URL Parameters @@ -16,7 +16,10 @@ def download(url, checksum=None, **kwargs): URL from which to download checksum : str or None MD5 checksum to check against - Keyword arguments passed to :func:urllib.request.urlopen + as_browser : bool + Change User-Agent header to appear as a browser + kwargs : dict + Keyword arguments passed to :func:urllib.request.urlopen Returns ------- @@ -24,7 +27,11 @@ def download(url, checksum=None, **kwargs): Name of file written locally """ - req = urlopen(url, **kwargs) + if as_browser: + page = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) + else: + page = url + req = urlopen(page, **kwargs) # Get file size from header file_size = req.length