Skip to content

Instantly share code, notes, and snippets.

@tantale
Created June 7, 2017 05:30
Show Gist options
  • Save tantale/5081c098d7ba59cd6a86c6639546f2b6 to your computer and use it in GitHub Desktop.
Save tantale/5081c098d7ba59cd6a86c6639546f2b6 to your computer and use it in GitHub Desktop.
Simple Python 2.7 equivalent *wget* function with progress bar.
# coding: utf-8
import io
import urllib2
def get_file(src_url, dst_path, callback=None):
with io.open(dst_path, mode="wb") as dst_file:
get_stream(src_url, dst_file, callback)
def get_stream(src_url, dst_file, callback=None, buffer_size=io.DEFAULT_BUFFER_SIZE):
if callback is None:
def callback(cz, sz):
pass
response = urllib2.urlopen(src_url)
meta = response.info()
file_size = int(meta.getheader("Content-Length", 0))
chunk_size = 0
read_size = buffer_size
callback(chunk_size, file_size)
while read_size == buffer_size:
chunk = response.read(buffer_size)
dst_file.write(chunk)
read_size = len(chunk)
chunk_size += read_size
callback(chunk_size, file_size)
if __name__ == '__main__':
my_file = io.BytesIO()
def my_callback(cz, fz):
if fz:
print(r"{:10d} [{:3.2f}%]".format(cz, cz * 100. / fz))
else:
print(r"{:10d}]".format(cz))
get_stream("http://google.com", my_file, my_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment