import requests


def download_file(url, local_filename):
    """
    Use requests to stream the file from the server in chunks
    Please see this SO thread for more information and source
    https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests

    :param url: the url of the file to download
    :param local_filename: the name of the file to write
    :return: the local file name of the saved file
    """

    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)
    return local_filename