我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用google.appengine.api.urlfetch.DownloadError()。
def request(self, method, url, body, headers): # Calculate the absolute URI, which fetch requires netloc = self.host if self.port: netloc = '%s:%s' % (self.host, self.port) absolute_uri = '%s://%s%s' % (self.scheme, netloc, url) try: response = fetch(absolute_uri, payload=body, method=method, headers=headers, allow_truncated=False, follow_redirects=False, deadline=self.timeout, validate_certificate=self.validate_certificate) self.response = ResponseDict(response.headers) self.response['status'] = str(response.status_code) self.response['reason'] = httplib.responses.get(response.status_code, 'Ok') self.response.status = response.status_code setattr(self.response, 'read', lambda : response.content) # Make sure the exceptions raised match the exceptions expected. except InvalidURLError: raise socket.gaierror('') except (DownloadError, ResponseTooLargeError, SSLCertificateError): raise httplib.HTTPException()
def do_request_async(self, url, method='GET', headers=None, payload=None, deadline=None, callback=None): """Inherit docs. This method translates urlfetch exceptions to more service specific ones. """ if headers is None: headers = {} if 'x-goog-api-version' not in headers: headers['x-goog-api-version'] = '2' headers['accept-encoding'] = 'gzip, *' try: resp_tuple = yield super(_StorageApi, self).do_request_async( url, method=method, headers=headers, payload=payload, deadline=deadline, callback=callback) except urlfetch.DownloadError, e: raise errors.TimeoutError( 'Request to Google Cloud Storage timed out.', e) raise ndb.Return(resp_tuple)
def request(self, method, url, body, headers): # Calculate the absolute URI, which fetch requires netloc = self.host if self.port: netloc = '%s:%s' % (self.host, self.port) absolute_uri = '%s://%s%s' % (self.scheme, netloc, url) try: response = fetch(absolute_uri, payload=body, method=method, headers=headers, allow_truncated=False, follow_redirects=False, deadline=self.timeout, validate_certificate=self.validate_certificate) self.response = ResponseDict(response.headers) self.response['status'] = response.status_code setattr(self.response, 'read', lambda : response.content) # Make sure the exceptions raised match the exceptions expected. except InvalidURLError: raise socket.gaierror('') except (DownloadError, ResponseTooLargeError, SSLCertificateError): raise httplib.HTTPException()
def get_result(self): try: result = self.rpc.get_result() except urlfetch.DownloadError as e: self.log.error("Failed to retrieve %s (%s)", self.rpc.msg, repr(e)) return None msg = "{} {} (returned {} bytes)".format(self.rpc.msg, result.status_code, len(result.content)) json_result = json.loads(result.content) if result.status_code in self.valid_codes: self.log.debug(msg) self.log.debug(json.dumps( json_result, sort_keys=True, indent=4, separators=(',', ': '))) else: self.log.warning(msg) self.log.info(json.dumps( json_result, sort_keys=True, indent=4, separators=(',', ': '))) return json_result
def get(self): # [START urlfetch-rpc] rpc = urlfetch.create_rpc() urlfetch.make_fetch_call(rpc, 'http://www.google.com/') # ... do other things ... try: result = rpc.get_result() if result.status_code == 200: text = result.content self.response.write(text) else: self.response.status_int = result.status_code self.response.write('URL returned status code {}'.format( result.status_code)) except urlfetch.DownloadError: self.response.status_int = 500 self.response.write('Error fetching URL') # [END urlfetch-rpc]
def do_request_async(self, url, method='GET', headers=None, payload=None, deadline=None, callback=None): """Inherit docs. This method translates urlfetch exceptions to more service specific ones. """ if headers is None: headers = {} if 'x-goog-api-version' not in headers: headers['x-goog-api-version'] = '2' headers['accept-encoding'] = 'gzip, *' try: resp_tuple = yield super(_StorageApi, self).do_request_async( url, method=method, headers=headers, payload=payload, deadline=deadline, callback=callback) except urlfetch.DownloadError as e: raise errors.TimeoutError( 'Request to Google Cloud Storage timed out.', e) raise ndb.Return(resp_tuple)
def request(self, method, url, body, headers): # Calculate the absolute URI, which fetch requires netloc = self.host if self.port: netloc = '%s:%s' % (self.host, self.port) absolute_uri = '%s://%s%s' % (self.scheme, netloc, url) try: try: # 'body' can be a stream. body = body.read() except AttributeError: pass response = fetch(absolute_uri, payload=body, method=method, headers=headers, allow_truncated=False, follow_redirects=False, deadline=self.timeout, validate_certificate=self.validate_certificate) self.response = ResponseDict(response.headers) self.response['status'] = str(response.status_code) self.response['reason'] = httplib.responses.get(response.status_code, 'Ok') self.response.status = response.status_code setattr(self.response, 'read', lambda : response.content) # Make sure the exceptions raised match the exceptions expected. except InvalidURLError: raise socket.gaierror('') except (DownloadError, ResponseTooLargeError, SSLCertificateError): raise httplib.HTTPException()
def test_url_fetch_rpc_error(urlfetch_mock, app): urlfetch_mock.DownloadError = urlfetch.DownloadError get_result_mock = mock.Mock( side_effect=urlfetch.DownloadError()) urlfetch_mock.create_rpc = mock.Mock( return_value=mock.Mock(get_result=get_result_mock)) response = app.get('/', status=500) assert 'Error fetching URL' in response.body
def _map_rpc_to_data(object_rpc): try: result = object_rpc.get_result() if result.status_code != 200: logging.warning("BatchLookup: Error downloading, error code is %s, body is %s", result.status_code, result.content) if result.status_code in [200, 400]: text = result.content return json.loads(text) except urlfetch.DownloadError, e: logging.warning("BatchLookup: Error downloading: %s", e) return None
def get_result(self): results = [] if self.do_timeout: raise urlfetch.DownloadError("Deadline exceeded while waiting for HTTP response from URL") elif self.expired_token: return FakeResult( 400, { 'error': { 'message': u'Error validating access token: Session has expired on Jun 9, 2014 10:05am. The current time is Jun 9, 2014 10:32am.', 'code': 190, 'type': 'OAuthException', 'error_subcode': 463 } } ) else: urls = [x['relative_url'] for x in self.batch_list] for url in urls: if url in MemoryFBAPI.results: result = MemoryFBAPI.results[url] if result is RESULT_TIMEOUT: status_code = None content = None else: status_code, content = result else: logging.error('Returning 404 because url %s not in %s', url, MemoryFBAPI.results) status_code = 404 content = None if status_code: results.append(dict(code=status_code, body=json.dumps(content))) else: results.append(None) return FakeResult(200, results)
def get_download_URL( self, enkiDL_URL, secret, item_to_download, ip_addr ): form_fields = { 'item' : item_to_download, 'secret' : secret, 'ip_addr' : ip_addr } form_data = enki.libutil.urlencode( form_fields ) try: result = urlfetch.fetch( url = enkiDL_URL, payload = form_data, method = urlfetch.POST ) if result.status_code == 200: token = result.content self.download_url = enkiDL_URL + 'download?token=' + str( token ) + '&item=' + str( item_to_download ) else: self.error = 1 return except urlfetch.DownloadError: self.error = 2 return