【问题标题】:HTTPException: Deadline exceeded while waiting for HTTP response from URLHTTPException: 等待来自 URL 的 HTTP 响应时超过了最后期限
【发布时间】:2014-09-29 18:22:45
【问题描述】:

我的 Google App Engine 程序顶部有这段代码:

from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(60)

我正在使用开瓶器来加载东西:

    cj = CookieJar()        

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor( cj ) )
    opener.addheaders = [ ( 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64)' ) ]

    resp = opener.open( 'http://www.example.com/' )

5 秒后抛出异常:

 File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 1222, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/gae_override/httplib.py", line 524, in getresponse
    raise HTTPException(str(e))
HTTPException: Deadline exceeded while waiting for HTTP response from URL: http://www.example.com

如何避免错误?

【问题讨论】:

  • 如果您无法在 http 请求的年龄限制内完成此操作,另一种方法是将工作设置为 taskmapreduce job。这两种方式对方法和http请求有更灵活的时间限制。常规 gae http 请求有 15-20 秒的超时限制,因此如果您需要更多耗时的工作,还有其他选择。

标签: python google-app-engine urllib2 urlfetch


【解决方案1】:

您是否尝试设置.open() 调用的超时时间?

resp = opener.open('http://example.com', None, 60)

如果达到set_default_fetch_deadline 指定的超时时间,Python 将抛出DownloadErrorDeadlineExceededErrors 异常:https://cloud.google.com/appengine/docs/python/urlfetch/exceptions

【讨论】:

  • 不,我没试过,但我现在就试试!
【解决方案2】:

您还可以修补 httplib2 库并将截止日期设置为 60 秒

httplib2/__init__.py:
      def fixed_fetch(url, payload=None, method="GET", headers={},
                      allow_truncated=False, follow_redirects=True,
                      deadline=60):
          return fetch(url, payload=payload, method=method, headers=headers,
                       allow_truncated=allow_truncated,
                       follow_redirects=follow_redirects, deadline=60,
                       validate_certificate=validate_certificate)
      return fixed_fetch

这是一种变通方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2018-01-17
    相关资源
    最近更新 更多