Why am I getting an AttributeError when trying to print out

三世轮回 提交于 2019-12-20 18:42:31

问题


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

回答1:


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



回答2:


Because there is no such attribute. Try:

print str(e)

and you will get nice:

HTTP Error 404: Not Found



回答3:


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.



来源:https://stackoverflow.com/questions/7554846/why-am-i-getting-an-attributeerror-when-trying-to-print-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!