I am trying to catch an unresolved url given to a urllib request.
import urllib.request
def getSite(url):
try:
with urllib.request.urlopen(url, timeout=2) as r:
print(url, "was resolved!")
except:
print(url, "wasn't resolved...")
return
I would expect this to attempt a connection to the url and if there is no response in 2 seconds it throws the error and prints out that it isn't resolved. If it resolves in under 2 seconds, it response accordingly quickly. This is what I want to happen. I'd like each request to not last more than the time I prescribe.
As it stands, using a valid url provides a speedy response:
> getSite('http://stackoverflow.com')
> http://stackoverflow.com was resolved!
real 0m0.449s
user 0m0.063s
sys 0m0.063s
However, using an invalid url takes much longer than 2 seconds:
> getSite('http://thisisntarealwebaddress.com')
> http://thisisntarealwebaddress.com wasn't resolved...
real 0m18.605s
user 0m0.063s
sys 0m0.047s
What is the timeout parameter really doing, and how can I get the results I want?
Docs: https://docs.python.org/3.1/library/urllib.request.html