473,372 Members | 1,200 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,372 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 1841

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: 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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.