[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Urllib keyerror, confused

Adam W.

2/2/2008 11:48:00 AM

I took this script: http://aspn.activestate.com/ASPN/Cookbook/Python/Re...
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.amaz...
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")
1 Answer

Navtej Singh

2/2/2008 12:04:00 PM

0

possibly the server did not sent the 'Content-Length' header.

On Feb 2, 2008 5:18 PM, Adam W. <AWasilenko@gmail.com> wrote:
> I took this script: http://aspn.activestate.com/ASPN/Cookbook/Python/Re...
> 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.amaz...
> 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")
> --
> http://mail.python.org/mailman/listinfo/p...
>