Automatically resume a download w/ urllib?
chris_moffitt at yahoo.com
Wed Oct 24 04:29:02 CEST 2001
After Oleg pointed me in the right direction, I managed to put something
together too. Too bad someone else beat me to the punch! Still, the
solution I came up with is a little different. It just needs to override an
error handler in urllib.FancyURLopener.. Let me know what you think.
import urllib, os
"""Create sub-class in order to overide error 206. This error means a
partial file is being sent,
which is ok in this case. Do nothing with this error.
def http_error_206(self, url, fp, errcode, errmsg, headers, data=None):
loop = 1
dlFile = "testfile"
existSize = 0
myUrlclass = myURLOpener()
outputFile = open(dlFile,"ab")
existSize = os.path.getsize(dlFile)
#If the file exists, then only download the remainder
myUrlclass.addheader("range","bytes=%s-" % (existSize))
outputFile = open(dlFile,"wb")
webPage = myUrlclass.open("http://192.168.1.4/%s" % dlFile)
#If the file exists, but we already have the whole thing, don't download
if int(webPage.headers['content-length']) == existSize:
loop = 0
print "File already downloaded"
numBytes = 0
data = webPage.read(8192)
if not data:
numBytes = numBytes + len(data)
for k,v in webPage.headers.items():
print k, "=",v
print "copied", numBytes, "bytes from", webPage.url
"Oleg Broytmann" <phd at phd.pp.ru> wrote in message
news:mailman.1003831763.10689.python-list at python.org...
> On Tue, Oct 23, 2001 at 05:31:54PM +1300, Mark Rowe wrote:
> > After reading some RFC's and tweaking some Apache settings, I managed to
> > this working. I was only able to test it on my local server and it
> > to work fine. Any comments or improvements, feel free :)
> Thank you.
> > if existSize > 0:
> > h.putheader('Range', 'bytes=%d-' % (existSize, ))
> Looks good.
> > ## HTTP error 416 = Request Range not Satisiable
> Typo (-: _|
> 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