DeadlineExceededError: ApplicationError: 5 in using the urllib2.urlopen () function

In my application, I use the urllib2.urlopen () function to call the api and get the result from this api. But this does not work fine. Sometimes it shows the result, but sometimes it gives the following error:

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 

I saw a try-except method for this, but it works for my code. My code block:

  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') 

I import:

 from google.appengine.runtime import DeadlineExceededError 

From stackoverflow, I got that its bcause server did not respond at the specified time, and we can handle the ..am exception, but we cannot. Any help would be greatly appreciated. Thank you for your help.

+4
source share
2 answers

The default timeout for URL fetch requests is only 5 seconds, so you can increase it using urlfetch directly:

 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') 

If you still find it superior, consider using asynchronous requests or move the logic to the task queue.

+12
source

As you said, the error occurs because you did not receive a response on time, and the request exceeded the deadline.

What you can do is move the request to the task queue, because tasks can take much longer.

How to catch an exception, did you try to add a return statement immediately after self.redirect?

0
source

All Articles