473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1840

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
1
by: Erling Ringen Elvsrud | last post by:
Hello, My aim is to write a small application to use free sms-sending services in a more convenient way than with a web-browser. I found: http://wwwsearch.sourceforge.net/mechanize/ (which...
2
by: john.lehmann | last post by:
Attacked is a piece of code which first hits the login page successfully and receives back login cookies. But then when I attempt to hit a page which is restricted to logged in users only, I fail....
13
by: Pelmen | last post by:
How can I get rid of recursive call __getattr__ inside this method, if i need to use method or property of the class?
0
by: guilligan.geo | last post by:
Hello, I'm trying to create an addin for Outlook 2002 using the one provided in the demo of win32com as a starting point. I've been able to do my addin and test it if I go the "standard" way...
5
by: diegososa | last post by:
I have this code inside a function: try: for linea in subprocess.Popen(comando, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).stdout: req =...
0
by: godavemon | last post by:
I'm using urllib2 to pull pages for a custom version of a web proxy and am having issues with 404 errors. Urllib2 does a great job of letting me know that a 404 happened with the following code. ...
1
by: Albert Hopkins | last post by:
On Mon, 2008-11-17 at 13:59 -0800, godavemon wrote: can treat an HTTP error as an exceptional event or a valid response: import urllib2 url = 'http://cnn.com/asfsdafsadfasdf/' try: page =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.