jmquintana79
4/12/2017 - 10:06 AM

Download file by http

Download file by http

import sys
import requests
from datetime import datetime, timedelta

# current time
tic = datetime.now()


""" PARAMETERS """
link = "http://mandeo.meteogalicia.es/thredds/fileServer/modelos/WRF_HIST/d03/2017/03/wrf_arw_det_history_d03_20170331_1200.nc4"
file_name = "result.nc4"

""" DOWNLOADER """

with open(file_name, "wb") as f:
        print("Downloading %s" % file_name)
        response = requests.get(link, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None: # no content length header
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )    
                sys.stdout.flush()
                

""" FOOT """

# calculate time of processing
toc = datetime.now()
tictoc = ((toc-tic).seconds)/60. # minutes
print ("\nProcess start: %s"%tic)
print ("Process finish: %s"%toc)
print ("Process time spent %s minutes"%tictoc)

print ("\n--- The End ---")
quit()