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\Py thon\Updater\Cl ient\test.py", line 1, in ?
import os, sys, urllib2
File "c:\Gigi\Dev\Py thon\Updater\Cl ient\test.py", line 36, in ?
download(url, filename, chunkSize)
File "c:\Gigi\Dev\Py thon\Updater\Cl ient\test.py", line 4, in download
fin = urllib2.urlopen (url)
File "C:\Python24\Li b\urllib2.py", line 130, in urlopen
return _opener.open(ur l, data)
File "C:\Python24\Li b\urllib2.py", line 364, in open
response = meth(req, response)
File "C:\Python24\Li b\urllib2.py", line 471, in http_response
response = self.parent.err or(
File "C:\Python24\Li b\urllib2.py", line 396, in error
result = self._call_chai n(*args)
File "C:\Python24\Li b\urllib2.py", line 337, in _call_chain
result = func(*args)
File "C:\Python24\Li b\urllib2.py", line 539, in http_error_302
if hasattr(req, 'redirect_dict' ):
File "C:\Python24\Li b\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(par ent_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(chunkS ize)
readBytes = len(chunk)
downloadBytes += readBytes
bytesLeft -= readBytes
fout.write(chun k)
print 'Progress: %d/%d' % (downloadBytes, totalBytes)
print 'Done.'
if __name__=="__ma in__":
#url = 'http://audacity.source forge.net/audacity-manual-1.2.zip'
url =
'http://www-users.york.ac.u k/~raa110/audacity/audacity-manual-1.2.3.zip'
url='http://download.mozill a.org/?product=thunde rbird-1.0.2&os=win&la ng=en-US'
url='http://download.mozill a.org/?product=thunde rbird-1.0.6&os=win&la ng=en-US'
filename = url.split('/')[-1]
chunkSize = 1
download(url, filename, chunkSize)
print open(filename). read()
Thanks, Gil 0 1850 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 :",attr,unexpected,self.__dict__
return self.__dict__
class derived(base):
def __setattr__(self,attr,key,*unexpected):
|
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):
funcname = 'do_' + cmd
if hasattr(self, funcname):
getattr(self, funcname)()
|
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 resemples the perl
variant). With mechanize I should manage to interact with the website
through python, like supplying usernames, filling the message...
|
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.
That seems to be because I am not successfully re-attaching the cookies
to the header portion of the this request. I have tried 2 methods
which...
|
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?
| |
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 (using the python
interpreter). But now, I want other poeple to use it to I want to
freeze it for windows using py2exe and py2exe is giving me a...
|
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 = urllib2.Request(URL, "&mac_addr=" + mac_addr + " &sampled_time=" + sampled_time + " &line_data=" + linea)
ret = urllib2.urlopen(req).read()
except...
|
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.
import urllib2
url = 'http://cnn.com/asfsdafsadfasdf/'
try:
page = urllib2.urlopen( url )
except urllib2.URLError, e:
print e
|
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 = urllib2.urlopen(url)
except urllib2.URLError, e:
print e.read()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |