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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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#...
|
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,...
|
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...
|
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.....
|
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...
| |
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...
|
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 =...
|
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...
|
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:
...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |