Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python HTTPException:等待URL的HTTP响应时超过了截止日期_Python_Google App Engine_Urllib2_Urlfetch - Fatal编程技术网

Python HTTPException:等待URL的HTTP响应时超过了截止日期

Python HTTPException:等待URL的HTTP响应时超过了截止日期,python,google-app-engine,urllib2,urlfetch,Python,Google App Engine,Urllib2,Urlfetch,我的Google应用程序引擎程序顶部有以下代码: from google.appengine.api import urlfetch urlfetch.set_default_fetch_deadline(60) 我用开瓶器来装东西: cj = CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor( cj ) ) opener.addheaders = [ ( 'Us

我的Google应用程序引擎程序顶部有以下代码:

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

如何避免此错误?

您是否尝试在
.open()
调用上设置超时

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

如果达到
set\u default\u fetch\u deadline
指定的超时,Python将抛出
DownloadError
deadlineExceederErrors
异常:

您还可以修补httplib2库并将deadline设置为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

这是一个变通办法。

不,我没有试过,但我现在就要试!如果不能在http请求的时间限制内完成,另一种方法是将作业设为a或a。这两种方法对方法和http请求有更灵活的时间限制。常规gae http请求有15-20秒的超时限制,因此如果您需要更耗时的作业,还有其他选择。