Note that there are some explanatory texts on larger screens.

plurals
  1. POPython 3.3: Catching HTTPError with Timer
    text
    copied!<p>I've been trying to study python and I wanted to try catching a server time out error (in case my internet connection is interrupted or something) and adding a timer that will make the process stop for X seconds (in my code below it is 2 seconds) then try to continue the process again. Although I believe my logic is right (please do correct me if I'm wrong), I get an error that says </p> <pre><code>Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python33\lib\threading.py", line 639, in _bootstrap_inner self.run() File "C:\Python33\lib\threading.py", line 825, in run self.function(*self.args, **self.kwargs) TypeError: 'NoneType' object is not callable </code></pre> <p>I do not quite understand why my timer seems to trigger and error. Here is my code:</p> <pre><code>import urllib.request from threading import Timer req = urllib.request.Request('http://www.nowebsitecontainsthisaddress.com') def ServerTimeOut(e): timer_rest = Timer(2.0, print('Time Out Error:')) timer_rest.start() print (e.reason) while timer_rest.is_alive(): pass while True: try: url = urllib.request.urlopen(req) break except urllib.error.HTTPError as e: ServerTimeOut(e) req = urllib.request.Request('http://www.google.com') except urllib.error.URLError as e: ServerTimeOut(e) req = urllib.request.Request('http://www.google.com') </code></pre> <p>OS Used is windows7</p> <p>Update: Hi Guys, I tried tweaking my code into this and no more error is return. I do hope someone could enligthen me as to why the error was raised.. Thanks ^^,</p> <pre><code>import urllib.request from threading import Timer req = urllib.request.Request('http://www.nowebsitecontainsthisaddress.com') def printMessage(): print('Time Out Error:') def ServerTimeOut(e): timer_rest = Timer(2.0, printMessage) timer_rest.start() print (e.reason) while timer_rest.is_alive(): pass while True: try: url = urllib.request.urlopen(req) break except urllib.error.HTTPError as e: ServerTimeOut(e) req = urllib.request.Request('http://www.google.com') except urllib.error.URLError as e: ServerTimeOut(e) req = urllib.request.Request('http://www.google.com') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload