好球员,我在谷歌搜索,并在这里对计算器这个答案,并在数小时后,没有看到一个工作脚本来做到这一点的一个正确的答案....蟒蛇的urllib2超时
在这里,我粘贴假设的python工作脚本的4个示例,为超时设置的套接字和/或超时参数设置不存在的url的默认超时值。
没有人工作,所以超时从不触发。
任何想法?
首先〔实施例:
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!"
第二个例子:
import urllib2
try:
response = urllib2.urlopen("http://www.nonexistantdomainurl.com/notexist.php", None, 2.5)
except urllib2.URLError, e:
print "Oops, timed out?"
THRID例如:
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
第四个例子:
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
可能的解释:http://stackoverflow.com/a/8028602/190597 – unutbu 2012-02-14 12:55:35
谢谢@unutbu但这是另一种情况与超时问题无关。在那里,她想知道如何在发送不好的地点时变成200。 – dyoser 2012-02-14 14:56:35