python - 如何处理 urllib2 套接字超时?

标签 python exception urllib2 urllib continue

因此,以下内容适用于已超时的其他链接,并继续到循环中的下一个链接。但是对于这个链接我遇到了错误。我不确定这是为什么以及如何修复它,以便当它发生时它只是浏览到下一个图像。

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

最佳答案

显式捕获超时异常: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

关于python - 如何处理 urllib2 套接字超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39989350/

相关文章:

python - 在 Google App Engine 中使用 urllib2 会抛出 "Deadline exceeded while waiting for HTTP response from URL:..."

python - 如何检测是否安装了numpy

Android 捕获应用程序崩溃

python - BeautifulSoup:抓取嵌入的 href 链接列表

ruby-on-rails - InvalidAuthenticityToken in Devise::SessionsController#destroy(已退出后退出)

c# - 如何将空引用分析的结果写入日志文件

python - httplib 与 urllib2 和 cookies

python - xlwings 问题写入 Excel 2010 中的单元格

python - 如何在 python 中使用两个列表创建字典?

python - Sqlite:无法选择具有大整数 id 的行