Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 912699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:25:41+00:00 2026-05-15T17:25:41+00:00

how can I catch the 404 and 403 errors for pages in python and

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:25:42+00:00Added an answer on May 15, 2026 at 5:25 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any addon by which I can disable all catch blocks temporarily. I'm
Is there any way I can catch the event when I press the back
I am trying to make custom error pages for 404, 403, 5xx errors. I
We know that we can catch any unexpected exception at application level by using
Is there some way I can catch when a method generates a write to
I tried to use <error-page> <exception-type>java.lang.Exception</exception-type> <location>/errors/error.jsp</location> </error-page> but i dosen't catch 404 errors.
I'd like to handle HTML 404 errors in my web app. I can write
How can I catch all application exceptions and PHP errors in production environment? I
Is there any way to catch the exception from UrlFetchApp.fetch ? I thought I
I've got an application, which uses Winsock 2.0 recv function, and I can catch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.