Catching http errors
Asked Answered
M

2

13

how can I catch the 404 and 403 errors for pages in python and urllib(2), for example?

Are there any fast ways without big class-wrappers?

Added info (stack trace):

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    page = urllib2.urlopen("http://localhost:4444")
  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 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
Millsap answered 15/7, 2010 at 14:33 Comment(0)
P
23
import urllib2 
try:
   page = urllib2.urlopen("some url")
except urllib2.HTTPError, err:
   if err.code == 404:
       print "Page not found!"
   elif err.code == 403:
       print "Access denied!"
   else:
       print "Something happened! Error code", err.code
except urllib2.URLError, err:
    print "Some other error happened:", err.reason

In your case, the error happens already before the HTTP connection could be built - therefore you need to add another error handler that catches URLError. But this has nothing to do with 404 or 403 errors.

Panda answered 15/7, 2010 at 14:36 Comment(5)
This works for 403, but not 404. urllib2.URLError: <urlopen error [Errno 111] Connection refused>Millsap
That's because it's not a 404 error you're seeing. The error message says "Connection refused" - not "Page not found".Panda
Yeah, you are right. But there is also long exception trace before it. So that is uncatched exception, right? The main problem is catching exact that error. Could you help with this too?Millsap
Can you post the stack trace in your question?Panda
The comma after HTTPError gives me a syntax error (python 3.6 & urllib)Ezzell
P
5
req = urllib2.Request('url')
>>> try:
>>>     urllib2.urlopen(req)
>>> except urllib2.URLError, e:
>>>     print e.code
>>>     print e.read()
Pallette answered 15/7, 2010 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.