473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do HTTPError objects always have a read method?

I understood that HTTPError objects always have a read method, so they
can be treated as if they were a webpage. E.g. something like this
simplified snippet:
address = 'http://www.yahoo.com/spamspamspamspamspam'
try:
conn = urllib2.urlopen(address)
except urllib2.HTTPError, e:
conn = e
print conn.read() # Print the requested page, or the error page.

But in the source code to urllib2 (Python 2.5):
class HTTPError(URLError, addinfourl):
"""Raised when HTTP error occurs, but also acts like non-error
return"""
__super_init = addinfourl.__init__

def __init__(self, url, code, msg, hdrs, fp):
self.code = code
self.msg = msg
self.hdrs = hdrs
self.fp = fp
self.filename = url
# The addinfourl classes depend on fp being a valid file
# object. In some cases, the HTTPError may not have a valid
# file object. If this happens, the simplest workaround is to
# not initialize the base classes.
if fp is not None:
self.__super_init(fp, hdrs, url)

def __str__(self):
return 'HTTP Error %s: %s' % (self.code, self.msg)

That tells me that HTTPError objects aren't guaranteed to include a file-
like interface. That makes me unhappy.

Under what circumstances do HTTPError objects not have a valid file
object? How common is this? Does anyone have an example of a URL that
fails in that fashion?

Thank you,

--
Steven
Sep 18 '08 #1
2 1961
En Wed, 17 Sep 2008 22:48:27 -0300, Steven D'Aprano
<st****@remove.this.cybersource.com.auescribió:
I understood that HTTPError objects always have a read method, so they
can be treated as if they were a webpage. E.g. something like this
simplified snippet:
address = 'http://www.yahoo.com/spamspamspamspamspam'
try:
conn = urllib2.urlopen(address)
except urllib2.HTTPError, e:
conn = e
print conn.read() # Print the requested page, or the error page.

But in the source code to urllib2 (Python 2.5):
class HTTPError(URLError, addinfourl):
"""Raised when HTTP error occurs, but also acts like non-error
return"""
__super_init = addinfourl.__init__

def __init__(self, url, code, msg, hdrs, fp):
self.code = code
self.msg = msg
self.hdrs = hdrs
self.fp = fp
self.filename = url
# The addinfourl classes depend on fp being a valid file
# object. In some cases, the HTTPError may not have a valid
# file object. If this happens, the simplest workaround is to
# not initialize the base classes.
if fp is not None:
self.__super_init(fp, hdrs, url)

That tells me that HTTPError objects aren't guaranteed to include a file-
like interface. That makes me unhappy.

Under what circumstances do HTTPError objects not have a valid file
object? How common is this? Does anyone have an example of a URL that
fails in that fashion?
Well, there is at least one case (AbstractDigestAuthHandler at line 864 in
urllib2.py) where HTTPError is raised explicitely with fp=None. It's not a
common case, I think. If you rely on the exception having a read() method,
add it a fake one yourself:

try: ...
except urllib2.HTTPError, e:
if not hasattr(e, 'read'):
e.read = e.readline = lambda self: '' # or perhaps e.msg
conn = e

--
Gabriel Genellina

Sep 19 '08 #2
En Thu, 18 Sep 2008 23:12:22 -0300, Gabriel Genellina
<ga*******@yahoo.com.arescribió:
En Wed, 17 Sep 2008 22:48:27 -0300, Steven D'Aprano
<st****@remove.this.cybersource.com.auescribió:
>Under what circumstances do HTTPError objects not have a valid file
object? How common is this? Does anyone have an example of a URL that
fails in that fashion?

Well, there is at least one case (AbstractDigestAuthHandler at line 864
in urllib2.py) where HTTPError is raised explicitely with fp=None. It's
not a common case, I think. If you rely on the exception having a read()
method, add it a fake one yourself:

try: ...
except urllib2.HTTPError, e:
if not hasattr(e, 'read'):
e.read = e.readline = lambda self: '' # or perhaps e.msg
conn = e
Oops, that should have been e.read = lambda: ''

--
Gabriel Genellina

Sep 19 '08 #3

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

Similar topics

48
3426
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
8
1535
by: JSheble | last post by:
I'm writing some code that interacts with MSMQ, and am getting in the habit of using try{}catch{}finally{} blocks, but am now running into a problem with un-initialized variables... for example,...
14
2482
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
161
7684
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
10
1876
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
25
2737
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as...
1
3349
by: Magnus.Moraberg | last post by:
Hi, I have the following code - import urllib2 from BeautifulSoup import BeautifulSoup proxy_support = urllib2.ProxyHandler({"http":"http:// 999.999.999.999:8080"}) opener =...
23
5695
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
2
3904
by: silk.odyssey | last post by:
I am getting the following error trying to download an html page using urllib2. urllib2.HTTPError: HTTP Error 204: NoContent The url is of this type: ...
0
7199
marktang
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,...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7453
tracyyun
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...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
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...
0
3162
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
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 ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.