Skip to content

Instantly share code, notes, and snippets.

@shichao-an
Created September 17, 2014 18:27
Show Gist options
  • Save shichao-an/22f6a7f579bbf0ccfb6e to your computer and use it in GitHub Desktop.
Save shichao-an/22f6a7f579bbf0ccfb6e to your computer and use it in GitHub Desktop.
PycURL progress
import pycurl
import sys
from humanize import naturalsize
import time
START_TIME = None
def progress(download_t, download_d, upload_t, upload_d):
if int(download_t) == 0:
return
global START_TIME
if START_TIME is None:
START_TIME = time.time()
duration = time.time() - START_TIME + 1
speed = download_d / duration
speed_s = naturalsize(speed, binary=True)
speed_s += '/s'
if int(download_d) == 0:
download_d == 0.01
p = '%s/%s (%.2f%%) %s %s\r' % (naturalsize(download_d, binary=True),
naturalsize(download_t, binary=True),
download_d / download_t, speed_s, ' ' * 10)
sys.stderr.write(p)
sys.stderr.flush()
with open('test.zip', 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, "http://download.thinkbroadband.com/10MB.zip")
c.setopt(c.WRITEDATA, f)
c.setopt(c.NOPROGRESS, 0)
c.setopt(c.PROGRESSFUNCTION, progress)
c.perform()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment