Skip to content

Instantly share code, notes, and snippets.

@erdemarslan
Last active April 9, 2022 19:30
Show Gist options
  • Save erdemarslan/e8a1804b2c5de316803f1c0bb1daf579 to your computer and use it in GitHub Desktop.
Save erdemarslan/e8a1804b2c5de316803f1c0bb1daf579 to your computer and use it in GitHub Desktop.
Download any file with percent on Python
import certifi
import urllib3
url = "http://www.serveraddress.tld/file/to/download.zip"
file_name = url.split('/')[-1]
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where()
)
r = http.request('GET', url, preload_content=False)
file_size = int(r.headers["Content-Length"])
print("Downloading: {} Bytes: {}".format(file_name, file_size))
file_size_dl = 0
block_sz = 8192
f = open(file_name, "wb")
while True:
buffer = r.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = "{}".format(int(file_size_dl * 100. // file_size))
#status = status + chr(8)*(len(status)+1)
print(status)
f.close()
@myoun
Copy link

myoun commented Sep 12, 2021

Good Code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment