Urllib keyerror, confused
Navtej Singh
n.s.buttar at gmail.com
Sat Feb 2 07:03:30 EST 2008
possibly the server did not sent the 'Content-Length' header.
On Feb 2, 2008 5:18 PM, Adam W. <AWasilenko at gmail.com> wrote:
> 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")
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list