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 7032797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:58:53+00:00 2026-05-28T00:58:53+00:00

First off, my problem is quite similar to this one . I would like

  • 0

First off, my problem is quite similar to this one. I would like a timeout of urllib.urlopen() to generate an exception that I can handle.

Doesn’t this fall under URLError?

try:
    response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
except (HTTPError, URLError) as error:
    logging.error(
        'Data of %s not retrieved because %s\nURL: %s', name, error, url)
else:
    logging.info('Access successful.')

The error message:

resp = urllib.request.urlopen(req, timeout=10).read().decode(‘utf-8’)
File “/usr/lib/python3.2/urllib/request.py”, line 138, in urlopen
return opener.open(url, data, timeout)
File “/usr/lib/python3.2/urllib/request.py”, line 369, in open
response = self._open(req, data)
File “/usr/lib/python3.2/urllib/request.py”, line 387, in _open
‘_open’, req)
File “/usr/lib/python3.2/urllib/request.py”, line 347, in _call_chain
result = func(*args)
File “/usr/lib/python3.2/urllib/request.py”, line 1156, in http_open
return self.do_open(http.client.HTTPConnection, req)
File “/usr/lib/python3.2/urllib/request.py”, line 1141, in do_open
r = h.getresponse()
File “/usr/lib/python3.2/http/client.py”, line 1046, in getresponse
response.begin()
File “/usr/lib/python3.2/http/client.py”, line 346, in begin
version, status, reason = self._read_status()
File “/usr/lib/python3.2/http/client.py”, line 308, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), “iso-8859-1”)
File “/usr/lib/python3.2/socket.py”, line 276, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out

There was a major change from in Python 3 when they re-organised the urllib and urllib2 modules into urllib. Is it possible that there was a change then that causes this?

  • 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-28T00:58:53+00:00Added an answer on May 28, 2026 at 12:58 am

    Catch the different exceptions with explicit clauses, and check the reason for the exception with URLError (thank you Régis B. and Daniel Andrzejewski)

    from socket import timeout
    from urllib.error import HTTPError, URLError
    
    try:
        response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
    except HTTPError as error:
        logging.error('HTTP Error: Data of %s not retrieved because %s\nURL: %s', name, error, url)
    except URLError as error:
        if isinstance(error.reason, timeout):
            logging.error('Timeout Error: Data of %s not retrieved because %s\nURL: %s', name, error, url)
        else:
            logging.error('URL Error: Data of %s not retrieved because %s\nURL: %s', name, error, url)
    else:
        logging.info('Access successful.')
    

    NB For recent comments, the original post referenced python 3.2 where you needed to catch timeout errors explicitly with socket.timeout. For example

    
    
        # Warning - python 3.2 code
        from socket import timeout
        
        try:
            response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8')
        except timeout:
            logging.error('socket timed out - URL %s', url)
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, first off I'm not quite sure how to phrase my problem. This could
First off, this example will only work in a browser that supports :nth-child ,
First off - apologies if this or a similar question has been asked before.
First off, thanks to whomever is reading this. I have a very strange problem
First off, there's a bit of background to this issue available on my blog:
First off, I'm working on an app that's written such that some of your
first off thank you for the pointer that something.endsWith doesn't do what i was
Please forgive me if I don't explain this very well. First off, I am
Sorry for the vague title, but not quite sure how to summarize this one.
Maybe there's someone out in the ether that can help me with this one.

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.