Python 3.5.2: socket.timeout exception causes typeerror -


i'm bit of python newbie , first post stackoverflow please bear me. :) before posting have searched google , stackoverflow cant seem find similar issue.

i have script polls website , retrieves content. works fine hours if encounters socket timeout script throws typeerror though have exception it.

i'm sure missing obvious, cant put finger on it.

code:

timingout = 10  def get_url(url):   try:     sock = urllib.request.urlopen(url, timeout=timingout)     orig_html = sock.read()     html = orig_html.decode("utf-8", errors="ignore").encode('cp1252', errors='ignore')       sock.close()        except keyboardinterrupt:         # kill program if control-c pressed         sys.exit(0)   except urllib.error.urlerror e:     print("***error= page ", e.reason)     return   except timingout:     print("socket timed out - url: %s", url)   else:     # see if site down or errors eg: 404     if html == none:         print ("page contains no content!?!")         return ''     # see if site complaining     elif html == site_overload:         if _verbose:             print('error: many requests - sleeping 600 secs')         time.sleep(600)         return ''     # if not,     elif html:         return html 

error:

    return self._sock.recv_into(b) **socket.timeout: timed out**  during handling of above exception, exception occurred:  traceback (most recent call last):   file "test.py", line 201, in <module>     main()   file "test.py", line 140, in main     http_text = get_text(site_id)   file "test.py", line 110, in get_text     return get_url(url)   file "test.py", line 59, in get_url     except timingout: **typeerror: catching classes not inherit baseexception not allowed** 

thanks in advance suggestions & help!

it's caused trying use timingout catch exception. timingout integer object whereas except statement accepts objects inherited baseexception class.

remove except because doesn't anything. consider revising try statement include single operation. make troubleshooting easier , prevent having break try statements later on when bug occurs.


Comments