如何处理 urllib2 套接字超时?

Posted

技术标签:

【中文标题】如何处理 urllib2 套接字超时?【英文标题】:How to handle urllib2 socket timeouts? 【发布时间】:2016-10-12 01:55:47 【问题描述】:

因此,以下内容适用于其他已超时并继续到循环中的下一个链接的链接。但是对于这个链接,我得到了一个错误。我不知道为什么会这样以及如何解决它,以便当它发生时它只是浏览到下一张图片。

try:
    image_file = urllib2.urlopen(submission.url, timeout = 5)
    with open('/home/mona/computer_vision/image_retrieval/images/'
              + category + '/'
              + datetime.datetime.now().strftime('%y-%m-%d-%s')
              + submission.url[-5:], 'wb') as output_image:
        output_image.write(image_file.read())
except urllib2.URLError as e:
    print(e)
    continue

错误是:

[LOG] Done Getting http://i.imgur.com/b6fhEkWh.jpg
submission id is: 1skepf
[LOG] Getting url:  http://www.redbubble.com/people/crtjer/works/11181520-bling-giraffe
[LOG] Getting url:  http://www.youtube.com/watch?v=Y7iuOZVJhs0
[LOG] Getting url:  http://imgur.com/8a62PST
[LOG] Getting url:  http://www.youtube.com/watch?v=DFZFiFCsTc8
[LOG] Getting url:  http://i.imgur.com/QPpOFVv.jpg
[LOG] Done Getting http://i.imgur.com/QPpOFVv.jpg
submission id is: 1f3amu
[LOG] Getting url:  http://25.media.tumblr.com/tumblr_lstla7vqK71ql5q9zo1_500.jpg
Traceback (most recent call last):
  File "download.py", line 50, in <module>
    image_file = urllib2.urlopen(submission.url, timeout = 5)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 415, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 371, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
socket.timeout: timed out

【问题讨论】:

【参考方案1】:

显式捕获超时异常:https://docs.python.org/3/library/socket.html#socket.timeout

try:
    image_file = urllib2.urlopen(submission.url, timeout = 5)
except urllib2.URLError as e:
    print(e)
    continue
except socket.Timeouterror:
    print("timed out")
    # Your timeout handling code here...
else:
    with open('/home/mona/computer_vision/image_retrieval/images/'+category+'/' + datetime.datetime.now().strftime('%y-%m-%d-%s') + submission.url[-5:], 'wb') as output_image:
        output_image.write(image_file.read())

操作: 谢谢! 感谢您的建议,我得到了这些,Python2.7 解决了我的问题:

except socket.timeout as e:
    print(e)
    continue
except socket.error as e:
    print(e)
    continue

【讨论】:

我现在就试试。如果它没有发生错误,我会接受它。也感谢您编辑问题。 CTL+k 对我不起作用。你如何修复缩进? 我只是通过在文本编辑器中递减它来手动编辑它。可能有一些方法可以从 SO 网站中处理它,但我不确定那是什么。

以上是关于如何处理 urllib2 套接字超时?的主要内容,如果未能解决你的问题,请参考以下文章

从套接字读取时如何处理阻塞的 read() 调用?

套接字编程:如何处理带外数据

http 服务器将如何处理 html5 网络套接字?

如何处理高传入数据并将受限制的数据发送到 django-channels 中的 Web 套接字

您如何处理 Flash AS3 中的实时视频流?

更大的应用程序如何处理潜在的 (web)socket 端口冲突?