472,145 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

problems with hasattr() and custom __getattr__ inside urllib2

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:

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

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):
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
print 'Progress: %d/%d' % (downloadBytes, totalBytes)

print 'Done.'

if __name__=="__main__":

#url = 'http://audacity.sourceforge.net/audacity-manual-1.2.zip'
url =


filename = url.split('/')[-1]
chunkSize = 1
download(url, filename, chunkSize)
print open(filename).read()

Thanks, Gil

Aug 24 '05 #1
0 1749

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian Roberts | last post: by
1 post views Thread by Erling Ringen Elvsrud | last post: by
2 posts views Thread by john.lehmann | last post: by
13 posts views Thread by Pelmen | last post: by
reply views Thread by guilligan.geo | last post: by
reply views Thread by godavemon | last post: by
1 post views Thread by Albert Hopkins | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.