[PYTHON URLLIB2] HELP ME HELP ME HELP ME (Italian boy)
Wojtek Walczak
gminick at hacker.pl
Sat Dec 7 15:13:57 EST 2002
Dnia Thu, 05 Dec 2002 16:57:14 GMT, PigioŽ napisał(a):
> My problem is this: I would like modify urllib2 exception HTTPError to
> permit it returns more attributes that I can use to read web page souce
> code.
> I have understood that I must add args attribute such tuple of return
> element (but I don't have understood the reason) but, what can I do to add
> this attribute if HTTPError class doesn't declare its attributes at
> beginning?
I'm not absolutely sure what you're trying to achieve, but I think
you want to change that(i.e.):
"HTTP Error 404: Not Found"
generated by this code:
---
import urllib2
try:
a = urllib2.urlopen("http://www.python.org/blah.blah")
except urllib2.HTTPError, msg:
print "%s" % (msg)
---
into something more complicated, like this:
"HTTP error: <http://www.python.org/blah.blah>: Not Found (404)."
If I'm right, take a look at this code:
----
import urllib2
class h(urllib2.HTTPError):
def __init__(self, url, code, msg, hdrs, fp):
self.url = url
self.code = code
self.msg = msg
self.hdrs = hdrs
self.fp = fp
def __str__(self):
return 'HTTP error: <%s>: %s (%s).' % (self.url, self.msg, self.code)
urllib2.HTTPError = h
try:
a = urllib2.urlopen("http://www.python.org/blah.blah")
except h, msg:
print "%s" % (msg)
----
HTH.
ps. I'm not a master of subclassing, so, I think language purists will
improve correctness of that code above ;]
--
[ ] gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/ [ ]
[ "Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej." ]
More information about the Python-list
mailing list