def download_img(img_name,img_url,**kwargs): import requests from get_proxy import get_proxy img_name_jp2 = img_name+".jp2" img_url = img_url.replace("B04.jp2",img_name_jp2) print(img_url) while 1: try: proxy = get_proxy() print(proxy) r = requests.get(img_url,proxies=proxy,timeout=5,stream=True,**kwargs) tile = 'Tile'+img_name+'.jp2' with open(tile,'wb') as f: for chunk in r.iter_content(chunk_size=102400): if chunk: f.write(chunk) break except: pass