Urllib keyerror, confused

A

Adam W.

I took this script: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/83208
And decided to try it out, it works when I first download a file, and
when I try to resume a downloaded file, but if the file is already
downloaded, and I expect to see the print "File already downloaded"
message come up, I get a keyerror instead:

Traceback (most recent call last):
File "C:\Users\Adam\Desktop\ddd.py", line 26, in <module>
if int(webPage.headers['Content-Length']) == existSize:
File "C:\Python25\lib\rfc822.py", line 384, in __getitem__
return self.dict[name.lower()]
KeyError: 'content-length'

Why did the key disappear?

Here is the code I used (a little modified but functionally the same):

import urllib, os

class myURLOpener(urllib.FancyURLopener):
"""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):
pass

loop = 1
dlFile =
"Okidata_Digital_B4600_Black_and_White_Laser_PrinterlacDetail.jpg"
existSize = 0
myUrlclass = myURLOpener()
if os.path.exists(dlFile):
outputFile = open(dlFile,"ab")
existSize = os.path.getsize(dlFile)
#If the file exists, then only download the remainder
myUrlclass.addheader("Range","bytes=%s-" % (existSize))
else:
outputFile = open(dlFile,"wb")

webPage = myUrlclass.open("http://s3-external-1.amazonaws.com/
wootsaleimages/%s" % dlFile)

#If the file exists, but we already have the whole thing, don't
download again
print "flyby"
if int(webPage.headers['Content-Length']) == existSize:
loop = 0
print "File already downloaded"

numBytes = 0.0
numBytes += existSize
while loop:
data = webPage.read(8192)
if not data:
break
outputFile.write(data)
numBytes += len(data)
print (float(numBytes/int(webPage.headers['Content-Length']))*100)

webPage.close()
outputFile.close()

for k,v in webPage.headers.items():
print k, "=",v
print "copied", numBytes, "bytes from", webPage.url
raw_input("Cat")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top