开发者

Why am I getting an AttributeError when trying to print out

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 urlli开发者_C百科b2

req = urllib2.Request('http://www.pretend-o-server.org')
try:
    urllib2.urlopen(req)
except urllib2.URLError, e:
    print e.code


Depending on the error type, the object e may or may not carry that attribute.

In the link you provided there is a more complete example:

Number 2

from urllib2 import Request, urlopen, URLError
req = Request(someurl)
try:
    response = urlopen(req)
except URLError, e:
    if hasattr(e, 'reason'): # <--
        print 'We failed to reach a server.'
        print 'Reason: ', e.reason
    elif hasattr(e, 'code'): # <--
        print 'The server couldn\'t fulfill the request.'
        print 'Error code: ', e.code
else:
    # everything is fine


Because there is no such attribute. Try:

print str(e)

and you will get nice:

HTTP Error 404: Not Found


The reason I got the AttributeError was because I was using OpenDNS. Apparently even when you pass in a bogus URL, OpenDNS treats it like it exists. So after switching to Googles DNS server, I am getting the expected result which is:

[Errno -2] Name or service not known

Also I should mention the traceback I got for running this code which is everything excluding try and except

from urllib2 import Request, urlopen, URLError, HTTPError

req = Request('http://www.pretend_server.com')
    urlopen(req)

is this

Traceback (most recent call last):
  File "urlerror.py", line 5, in <module>
    urlopen(req)
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

which a kind gentle(wo)man? from IRC #python told me was highly strange and then asked if I was using OpenDNS to which I replied yes. So they suggested I switch it to Google's which I proceeded to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜