Last active
June 2, 2024 19:19
-
-
Save erdemarslan/e8a1804b2c5de316803f1c0bb1daf579 to your computer and use it in GitHub Desktop.
Download any file with percent on Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0'}) | |
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 = "{:.2f}".format(file_size_dl * 100. // file_size) | |
#status = status + chr(8)*(len(status)+1) | |
print(status) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good Code!