I am learning about urllib2 by following this tutorial http://docs.python.org/howto/urllib2.html#urlerror Running the code below yields a different outcome from the tutorial
import urllib2
req = urllib2.Request('http://www.pretend-o-server.org')
try:
urllib2.urlopen(req)
except urllib2.URLError, e:
print e.reason
Python interpreter spits this back
Traceback (most recent call last):
File "urlerror.py", line 8, in <module>
print e.reason
AttributeError: 'HTTPError' object has no attribute 'reason'
How come this is happening?
UPDATE
When I try to print out the code attribute it works fine
import urllib2
req = urllib2.Request('http://www.pretend-o-server.org')
try:
urllib2.urlopen(req)
except urllib2.URLError, e:
print e.code
print e.reason
you putprint e.code
? – Hankhankeurllib2
(and its methods). So everything is working predictably. When I doprint e.code
I get404
. @agf I'm using 2.6.5 and by the way, how did you get[Errno 11004] getaddrinfo failed
. I swear I was getting that same message early today, I took a break, came back, ran the same code again and just get the original AttributeError. I'm thinking ghost in the machine but perhaps I am missing something. – Machado