2011-06-23 109 views
4

在我的应用程序中,我使用urllib2.urlopen()函数来调用api并从该api获取结果。但这不能正常工作。有时它显示结果,但有时它给出以下错误:DeadlineExceededError:ApplicationError:5使用urllib2.urlopen()函数中的5使用

Traceback (most recent call last): 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 700, in __call__ 
handler.get(*groups) 
    File "/base/data/home/apps/s~malware-app/7.351334968546050391/main.py", line 505, in get 
f = urllib2.urlopen('http://whoapi.com/api-v1/?domain=%s&rtype=alexarank&apikey=xyz'% domain) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen 
    return _opener.open(url, data) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 381, in open 
response = self._open(req, data) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 399, in _open 
    '_open', req) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain 
result = func(*args) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1114, in http_open 
return self.do_open(httplib.HTTPConnection, req) 
    File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1087, in do_open 
r = h.getresponse() 
    File "/base/python_runtime/python_dist/lib/python2.5/httplib.py", line 197, in getresponse 
self._allow_truncated, self._follow_redirects) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 260, in fetch 
return rpc.get_result() 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result 
return self.__get_result_hook(self) 
    File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 364, in _get_fetch_result 
raise DeadlineExceededError(str(err)) 
DeadlineExceededError: ApplicationError: 5 

我看到try-except方法,但它为我的代码工作。 我的代码块:

try:      
    f = urllib2.urlopen('http://whoapi.com/api-v1/?domain=%s&rtype=serverip&apikey=xyzxyz'% domain) 
    ip = f.read() 
except DeadlineExceededError, e: 
    self.redirect('/error') 

我输入:

from google.appengine.runtime import DeadlineExceededError 

从计算器我得到了它的bcause服务器没有在指定的时间内响应,我们可以处理异常..am但无法做到这一点。 任何帮助,将不胜感激。 感谢您的帮助

+0

可能重复[无法处理使用UrlFetch时DeadlineExceededError](http://stackoverflow.com/questions/5738146/unable-to-handle-deadlineexceedederror-while-using-urlfetch) – geoffspear

+0

请不要发送重定向一个错误 - 只是服务500错误,然后在那里。 –

回答

12

的URL抓取请求默认的超时时间只有5秒钟,这样你可能想通过直接使用urlfetch增加它:

from google.appengine.api import urlfetch 

try: 
    resp = urlfetch.fetch('http://whoapi.com/api-v1/?domain=%s&rtype=serverip&apikey=xyzxyz'% domain, method=urlfetch.GET, deadline=10) 
    ip = r.content 
except urlfetch.DownloadError: 
    self.redirect('/error') 

如果您不断地发现超过也无妨,可以考虑使用asynchronous requests或移动逻辑到任务队列。

+0

谢谢你所有我们的答案... m在我的一个类中创建了4个urllib2.urlopen()请求...其中2个调用与urlfetch(){使用增加的时间}正常工作,但其他2个不使用urlfetch方法获取数据..所以必须使用urllib2。 urlopen(){任何想法为什么发生这种情况。任何想法来处理urllib2.urlopen()函数的错误。上面的尝试 - 除了没有工作fr urllib2.urlopen() – bitanalyzer

0

正如您所说,发生错误的原因是您没有及时得到答复,并且请求超出了截止日期。

您可以做的是将请求移动到任务队列中,因为任务可以运行更长时间。

至于捕捉异常,你有没有尝试在self.redirect之后添加return语句?

+0

不,我只是重定向它到错误类,但更早的时候我试图打印对象e,但没有打印任何东西......(这意味着它不会在除去块内) – bitanalyzer