GAE-AppEngine-deadlineexceederror:等待来自URL的HTTP响应时超过了截止日期:

2024-04-26 18:41:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Google AppEngine应用程序,它在我的本地机器上运行得很好。这个应用程序将一个图片(从一个url)发布到我的facebook墙上。但是,当我把它部署到Google的服务器上时 错误:

DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL: 

违规代码是:

facebook_access_token = facebook_info['access_token']

facebook_post_url = 'https://graph.facebook.com/me/photos?access_token=%s&url=%s&name=%s&method=post' % (facebook_access_token, url, caption)
facebook_post_url = facebook_post_url.replace(" ", "+");
facebook_result = urlfetch.fetch(facebook_post_url)

if facebook_result.status_code == 200:
  facebook_result_object = json.loads(facebook_result.content) 
  output_ids['facebook'] = facebook_result_object['id']
else:
  output_ids['facebook'] = ''

完整的错误跟踪是:

Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 710, in __call__
    handler.get(*groups)
  File "/base/data/home/apps/s~digibackapi/1.362663258877230387/main.py", line 512, in get
    facebook_result = urlfetch.fetch(facebook_post_url)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 266, in fetch
    return rpc.get_result()
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result
    return self.__get_result_hook(self)
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 404, in _get_fetch_result
    'Deadline exceeded while waiting for HTTP response from URL: ' + url)
DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL: https://graph.facebook.com/me/photos?access_token=<ACCESS_TOKEN>&url=http://digiback.cc/ej7&name=Trees&method=post

同样,代码在我看来是可靠的,在我的本地机器上也可以正常工作。这和超时有关系吗?当我在浏览器中尝试facebook_post_url时,它会立即返回。

有人有什么想法吗?我在这里完全失去了。

非常感谢!


Tags: inpytokenurlbasegetfacebookaccess
3条回答

简单回答:url获取的默认截止时间设置为5秒。

如何修复:

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)

我不熟悉facebook的API,但是urlfetch的构造方式看起来有点奇怪。通常,方法(Post)是一个urlphetch参数,Post负载是urlencoded。 这将导致:

params = urllib.urlencode([
    ('access_token', facebook_access_token),
    ('url', url),
    ('name', caption),
  ])
 response = urlfetch.fetch(url='https://graph.facebook.com/me/photos', payload=params, method=urlfetch.POST,
                      headers={'Content-Type': 'application/x-www-form-urlencoded'})

尝试将urlfetch的截止时间设置为30秒或更长(取决于您是在任务处理程序还是请求处理程序中调用urlfetch)

有关urlfetch的详细信息:Url Fetch Docs

相关问题 更多 >