Automatically resume a download w/ urllib?

Mark Rowe mark21rowe at yahoo.com
Tue Oct 23 00:31:54 EDT 2001


Hello,

After reading some RFC's and tweaking some Apache settings, I managed to get
this working.  I was only able to test it on my local server and it appears
to work fine.  Any comments or improvements, feel free :)

Mark Rowe


import httplib, os, sys

def resumeDL(local, remote):
    """resumeDL(local, remote) - Resumes the download of the file specified
in
<remote> in the form of (<server>, <path>) into the local file <local>.  If
the local file is not found, it is created and the download is started."""

    existSize = 0
    if os.path.exists(local):
        outputFile = open(local, 'ab')
        existSize = os.path.getsize(local)
    else:
        outputFile = open(local, 'wb')

    h = httplib.HTTP(remote[0])
    h.putrequest('GET', remote[1])
    h.putheader('Accept', '*/*')
    h.putheader('User-Agent', 'PythonHTTPResumer/0.1')
    h.putheader('Host', remote[0])
    if existSize > 0:
        h.putheader('Range', 'bytes=%d-' % (existSize, ))
    h.endheaders()
    errcode, errmsg, headers = h.getreply()
    print 'Response: %d (%s)' % (errcode, errmsg)
    if errcode == 416:
        ## HTTP error 416 = Request Range not Satisiable
        print 'File already exists and is larger than the remote file'
        h.getfile().close()
        del h
        return

    f = h.getfile()
    try:
        while 1:
            temp = f.read(8192)  ## 8192 == 8kb.  Saves 8kb at a time
            if temp == '':
                break
            outputFile.write(temp)
            sys.stdout.write('.')
    finally:
        outputFile.close()
        f.close()
        sys.stdout.write('\n')

if __name__ == '__main__':
    resumeDL('sm.tar.gz', ('192.168.1.4', '/sm-cvs-20010519.tar.gz'))

"Oleg Broytmann" <phd at phd.pp.ru> wrote in message
news:mailman.1003669161.6104.python-list at python.org...
> On Sun, Oct 21, 2001 at 02:02:52AM +0000, Chris Moffitt wrote:
> > I'm trying to develop a small application (mostly for Windows if that
> > matters), than can download and install binary files stored on a web
server.
> >
> > I've been able to get urllib to download the file just fine, but what I
> > would like to do is be able to resume a download if it is cancelled for
> > some reason.  In other words, if I've downloaded 50k of a 200k file, I'd
> > like to just download the remaining 150k.
>
>    I think it is impossible with urllib.
>
> > Is this possible using HTTP and Apache?  If not, what about FTP?  If
>
>    It is possible with Apache (apache supports HTTP Ranges) and httplib,
> but you'll need some coding. Learn about HTTP headers, especially Ranges.
>    It is also possible with FTP and ftplib, but again, you'll need some
> coding, and not all FTP servers support download resuming. Learn about ftp
> commands.
>    We'll very much appreciate if you post your working code here. Good
luck.
>
> Oleg.
> --
>      Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
>            Programmers don't die, they just GOSUB without RETURN.
>





More information about the Python-list mailing list