2012-02-14 69 views
1

好球员,我在谷歌搜索,并在这里对计算器这个答案,并在数小时后,没有看到一个工作脚本来做到这一点的一个正确的答案....蟒蛇的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 
+0

可能的解释:http://stackoverflow.com/a/8028602/190597 – unutbu 2012-02-14 12:55:35

+0

谢谢@unutbu但这是另一种情况与超时问题无关。在那里,她想知道如何在发送不好的地点时变成200。 – dyoser 2012-02-14 14:56:35

回答

4
>>> import urllib2 
>>> import time 
>>> import contextlib 
>>> 
>>> def timeit(): 
... s = time.time() 
... try: 
...  yield 
... except urllib2.URLError: 
...  pass 
... print 'took %.3f secs' % (time.time() - s) 
... 
>>> timeit = contextlib.contextmanager(timeit) 
>>> with timeit(): 
... r = urllib2.urlopen('http://loc:8080', None, 2) 
... 
took 2.002 secs 
>>> with timeit(): 
... r = urllib2.urlopen('http://loc:8080', None, 5) 
... 
took 5.003 secs 
+0

不错的做法,但尝试使用此命令:r = urllib2.urlopen('http://www.faluquito.com/equipo.php',None.5)...并看看会发生什么。另外我在你的终端上试过你的例子,它永远不会达到2或5秒......它花费了0.145秒,所以...... – dyoser 2012-02-14 12:27:09

+0

@dyoser,别忘了明确指定协议'http://'。我试图连接到你的'http:// faluquito.com/equipo.php'。它经常需要大约10秒,我认为问题在于域名解析,连接似乎被委托给阻塞的OS例程,并且在控制位于Python之后立即退出。但我可能是错的。 – newtover 2012-02-14 13:52:33

+0

这是因为编辑没有显示它:)但它是(http:// www我的意思是)..是的,这似乎是像你说的那样工作,所以在某些情况下超时是无用的。 – dyoser 2012-02-14 14:26:04

1

如果你的机器有UNIX程序掏,你可以找出这样的不存在的网址:

import logging 
import subprocess 
import shlex 

logging.basicConfig(level = logging.DEBUG, 
        format = '%(asctime)s %(module)s %(levelname)s: %(message)s', 
        datefmt = '%M:%S') 
logger = logging.getLogger(__name__) 

urls = ['http://1.2.3.4', 
     "http://www.nonexistantdomainurl.com/notexist.php", 
     "http://www.faluquito.com/equipo.php", 
     'google.com'] 

nonexistent = ['63.251.179.13', '8.15.7.117'] 
for url in urls: 
    logger.info('Trying {u}'.format(u=url)) 

    proc = subprocess.Popen(shlex.split(
     'dig +short +time=1 +retry=0 {u}'.format(u = url)), 
          stdout = subprocess.PIPE, stderr = subprocess.PIPE) 
    out, err = proc.communicate() 
    out = out.splitlines() 
    logger.info(out) 
    if any(addr in nonexistent for addr in out): 
     logger.info('nonexistent\n') 
    else: 
     logger.info('success\n') 

在我的机器,这产生了:

00:57 test INFO: Trying http://1.2.3.4 
00:58 test INFO: ['63.251.179.13', '8.15.7.117'] 
00:58 test INFO: nonexistent 

00:58 test INFO: Trying http://www.nonexistantdomainurl.com/notexist.php 
00:58 test INFO: ['63.251.179.13', '8.15.7.117'] 
00:58 test INFO: nonexistent 

00:58 test INFO: Trying http://www.faluquito.com/equipo.php 
00:58 test INFO: ['63.251.179.13', '8.15.7.117'] 
00:58 test INFO: nonexistent 

00:58 test INFO: Trying google.com 
00:58 test INFO: ['72.14.204.113', '72.14.204.100', '72.14.204.138', '72.14.204.102', '72.14.204.101'] 
00:58 test INFO: success 

注意挖掘返回['63.251.179.13', '8.15.7.117']为不存在的网址。

我相信我的ISP正在将不存在的地址更改为63.251.179.13或8.15.7.117。你的ISP可能会做一些不同的事。在这种情况下,您可能必须将nonexistent更改为其他内容。