Hi, I use urllib2 to download a redirected url and I get an exception from the bowels of urllib2. It seems that urllib2 implements some super sophisticated self check and tries to control the access to attributes using lots of calls to hasattr(the builtin function) and a custom __getattr__() on the Request class that perfroms some checks and raises an AttributeError if it's not happy. The problem is that hasattr() according to the docs is supposed to eat the exception and simply return False, but in my case it surfaces to my code and stops the regular flow. Here is the output and my code:
Output ====== AttributeError: redirect_dict Traceback (innermost last): File "c:\Gigi\Dev\Python\Updater\Client\test.py", line 1, in ? import os, sys, urllib2 File "c:\Gigi\Dev\Python\Updater\Client\test.py", line 36, in ? download(url, filename, chunkSize) File "c:\Gigi\Dev\Python\Updater\Client\test.py", line 4, in download fin = urllib2.urlopen(url) File "C:\Python24\Lib\urllib2.py", line 130, in urlopen return _opener.open(url, data) File "C:\Python24\Lib\urllib2.py", line 364, in open response = meth(req, response) File "C:\Python24\Lib\urllib2.py", line 471, in http_response response = self.parent.error( File "C:\Python24\Lib\urllib2.py", line 396, in error result = self._call_chain(*args) File "C:\Python24\Lib\urllib2.py", line 337, in _call_chain result = func(*args) File "C:\Python24\Lib\urllib2.py", line 539, in http_error_302 if hasattr(req, 'redirect_dict'): File "C:\Python24\Lib\urllib2.py", line 207, in __getattr__ raise AttributeError, attr Code ==== import os, sys, urllib2 def download(url, filename, chunkSize): fin = urllib2.urlopen(url) parent_dir = os.path.dirname(filename) if parent_dir != '' and not os.path.exists(parent_dir): os.makedirs(parent_dir) fout = open(filename, 'wb') info = fin.info() print 'info:', info totalBytes = int(info['Content-Length']) downloadBytes = 0 bytesLeft = totalBytes while bytesLeft > 0: chunk = fin.read(chunkSize) readBytes = len(chunk) downloadBytes += readBytes bytesLeft -= readBytes fout.write(chunk) print 'Progress: %d/%d' % (downloadBytes, totalBytes) print 'Done.' if __name__=="__main__": #url = 'http://audacity.sourceforge.net/audacity-manual-1.2.zip' url = 'http://www-users.york.ac.uk/~raa110/audacity/audacity-manual-1.2.3.zip' url='http://download.mozilla.org/?product=thunderbird-1.0.2&os=win&lang=en-US' url='http://download.mozilla.org/?product=thunderbird-1.0.6&os=win&lang=en-US' filename = url.split('/')[-1] chunkSize = 1 download(url, filename, chunkSize) print open(filename).read() Thanks, Gil -- http://mail.python.org/mailman/listinfo/python-list