Ok guys, i've search in google and here in stackoverflow for this answer and after a few hours did not see a correct answer of a working script to do this....
Here i paste 4 examples of supposed python working scripts to set a default timeout for a non-exist url with a timeout set with sockets and/or the timeout param.
No one works so the timeout is never triggered.
Any ideas?
First exmaple:
import urllib2
try:
header_s = {"User-Agent":"Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"}
req = urllib2.Request("http://www.nonexistantdomainurl.com/notexist.php",headers = header_s)
print urllib2.urlopen(req, None, 5.0).read()
except urllib2.URLError, e:
print "Url Error: %r" % e
except Exception,e:
print "Fallo de tipo ",e
else:
print "all ok!"
Second example:
import urllib2
try:
response = urllib2.urlopen("http://www.nonexistantdomainurl.com/notexist.php", None, 2.5)
except urllib2.URLError, e:
print "Oops, timed out?"
Thrid example:
from urllib2 import Request, urlopen, URLError, HTTPError
import base64
req = Request('http://www.nonexistantdomainurl.com/notexist.php')
try:
response = urlopen(req,timeout=5.0)
except HTTPError, e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
Fourth example:
import urllib2
import socket
socket.setdefaulttimeout(5)
try:
response = urllib2.urlopen("http://www.faluquito.com/equipo.php",timeout=5.0).read()
except urllib2.URLError, e:
print "Url Error: %r" % e