[Errno socket error] timed out 求助。

[Errno socket error] timed out 求助。

使用urllib库下载东西,可是经常出现下面的错误。查了一下,别人建议设置超时时间长一些,socket.setsockettimeout(10),我已经把延时设为10妙了,可还经常出现这种情况,我想问问,能不能让程序一直卡在那了,直到可以访问为止??不考虑将超时设为无穷大....

  File "601-poco-restaurant.py", line 207, in sendContent
    returnValues = urllib.urlopen("http://192.168.2.200/spider/save",params)    # 发送
  File "/usr/local/lib/python2.5/urllib.py", line 84, in urlopen
    return opener.open(url, data)
  File "/usr/local/lib/python2.5/urllib.py", line 192, in open
    return getattr(self, name)(url, data)
  File "/usr/local/lib/python2.5/urllib.py", line 328, in open_http
    errcode, errmsg, headers = h.getreply()
  File "/usr/local/lib/python2.5/httplib.py", line 1199, in getreply
    response = self._conn.getresponse()
  File "/usr/local/lib/python2.5/httplib.py", line 928, in getresponse
    response.begin()
  File "/usr/local/lib/python2.5/httplib.py", line 385, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.5/httplib.py", line 343, in _read_status
    line = self.fp.readline()
  File "/usr/local/lib/python2.5/socket.py", line 331, in readline
    data = recv(1)
IOError: [Errno socket error] timed out
setdefaulttimeout(...)
    setdefaulttimeout(timeout)

    Set the default timeout in floating seconds for new socket objects.
    A value of None indicates that new socket objects have no timeout.
    When the socket module is first imported, the default is None.

设为None,第一次导入默认就是None
恩,这样可以。
但是我程序是用代理抓取的,如果代理突然不好用了,那程序不一直卡在那了吗。
你可以try呀,不行的话别用代理或者换个代理或者重试呗……
使用try不还是一个样?延时设为None了,不就永久等待了吗,直到有回应。
try:
    连接中.....
except:
    ....
捕获一下异常如果是这个超时异常就重试 行不行?
恩,怎么能呢?
比方说这样,没测试
try:
    sock = urllib.urlopen("http://www.google.com/")
except IOError,e:
    if e.message=="time out":   我不知道是不是这样写
        continue
恩,谢谢