469,328 Members | 1,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

problems with hasattr() and custom __getattr__ inside urllib2

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

Aug 24 '05 #1
0 1663

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
1 post views Thread by CARIGAR | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.