All pastes #973454 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-06 03:22 UTC
rendered paste body
#!/usr/bin/python

import httplib, time
from urlparse import urlparse


loc = "http://noslor/ubuntu-7.04-desktop-i386.iso"

pos = 0
step = 1048576

def get_piece():
    global pos
    pos += 1
    if pos > 100: # stop after 100MB
        return (0,0)
    return (pos*step, step)

file = open('output', 'w')

def main():
    global place, file
    connection = httplib.HTTPConnection(urlparse(loc)[1])
    while True:
        begin, size = get_piece()
        place = begin
        end = begin + size
        #print 'fetching %s bytes starting at %s and ending at %s' % (size, begin, end)
        if size <= 0:
            break
        else:
            #connection.request('GET', urlparse(loc)[2])
            connection.request('GET', urlparse(loc)[2],
                    headers={'Range':'bytes=%s-%s' % (begin, 
                    end)})
            response = connection.getresponse()

            while True:
                data = response.read(4096)
                if data == '':
                    break
                write(data)

                if place > end:
                    break

            connection.close()

def write(data):
    global place, file
    file.write(data)
    place = place + len(data)


if __name__ == '__main__':
    start_t = time.time()
    main()
    end_t = time.time()
    print end_t - start_t