All pastes #699238 Raw Edit

Downloading HTTP Files With Pyat

public python v1 · immutable
#699238 ·published 2007-09-16 05:50 UTC
rendered paste body
from urllib2 import urlopen# Open up remote and local fileoutfile = file(filename, 'wb')urlfile = urlopen(self.url)filesize = int(urlfile.info().get('Content-Length', None))# Report beginning of downloadprint 'Beginning download of: %s' % urlprint '\tSize:',if filesize is None:    print 'Unknown'else:    print "%d KB" % (self.filesize / 1024)print '\tTo Local File:',self.filenamestart_time = time.time()# Download our filetotal_read = 0while 1:    bytes = urlfile.read(Download.CHUNK_SIZE)    bytes_read = len(bytes)        # If its zero we have hit the end of the file    if 0 == bytes_read:        break    # Write our data to the file    outfile.write(bytes)        # Update status information    total_read += bytes_read    print_status(filesize, total_read)# Report download statstotal_time = time.time() - start_timetotal_read /= 1024print '\nDownloaded: %d KB in %.2f seconds for %.2f KBs' % (      total_read, total_time, total_read/total_time)