def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
python类HTTPError()的实例源码
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def request(method,url,params=None,data=None,context=None):
http_client = httpclient.HTTPClient()
if params is not None and len(params.keys())>0:
url+='?'
for key in params.keys():
url+="%s=%s&"%(key,params[key])
url=url[:-1]
if context is not None:
url = url.replace('http://','https://',1)
try:
request = httpclient.HTTPRequest(url=url,
method =method,
ssl_options=context,
body=data)
response = http_client.fetch(request)
http_client.close()
except httpclient.HTTPError as e:
if e.response is None:
return tornado_response(500,str(e))
return tornado_response(e.response.code,e.response.body)
if response is None:
return None
return tornado_response(response.code,response.body)
def test_perm_roles_decorator(http_client, base_url, app_base_handlers, monkeypatch):
with pytest.raises(HTTPError) as e:
await http_client.fetch(base_url + '/test/api_test_model_dec/', method='GET')
async def success_get_roles(self):
return ['admin']
monkeypatch.setattr(DecTestHandler, 'get_roles', success_get_roles)
res = await http_client.fetch(base_url + '/test/api_test_model_dec/', method='GET')
assert res.code == 200
async def error_is_auth(self):
return False
monkeypatch.setattr(DecTestHandler, 'is_auth', error_is_auth)
with pytest.raises(HTTPError) as e:
await http_client.fetch(base_url + '/test/api_test_model_dec/', method='GET')
def ping(self, path=None):
request = HTTPRequest(
url=self.endpoint + (path or self.api_path),
method='GET',
headers=self.headers,
follow_redirects=False,
request_timeout=100
)
try:
yield self.outbound_client.fetch(request)
except HTTPError as ex: # pragma: no cover
if ex.code == 307:
raise_from(MasterRedirect(
urlparse(ex.response.headers['location']).netloc), None)
except ConnectionRefusedError as ex: # pragma: no cover
log.debug("Problem reaching: %s" % self.endpoint)
raise ex
except Exception as ex: # pragma: no cover
log.debug("Unhandled exception when connecting to %s",
self.endpoint)
raise ex
def test_bad_request_error(req, msg):
inbound = HTTPInbound()
inbound.start(None)
client = AsyncHTTPClient()
req.url = 'http://localhost:%s' % inbound.port
req.method = 'POST'
req.body = ''
with pytest.raises(HTTPError) as e:
yield client.fetch(req)
e = e.value
assert e.code >= 400 and e.code <= 500
assert e.response.body == msg
def get_entity(repository_id, entity_type, entity_id):
"""
Get an entity from a repository
:param repository_id: the repository that contains the entity
:param entity_type: a valid entity type (asset, offer or agreement)
:param entity_id: the entity's ID
:returns: the entity data from the repository service
:raises: tornado.httpclient.HTTPError
"""
endpoint = yield entity_endpoint(repository_id, entity_type)
if not endpoint:
raise Return(None)
try:
result = yield endpoint[entity_id].get()
except httpclient.HTTPError as err:
if err.code == 404:
raise Return(None)
else:
raise
raise Return(result['data'])
def entity_endpoint(repository_id, entity_type):
"""
Return an chub.API endpoint for the repository & entity type
:param repository_id: the repository that contains the entity
:param entity_type: a valid entity type (asset, offer or agreement)
:returns: chub.API instance
:raises: tornado.httpclient.HTTPError
"""
try:
repository = yield common.get_repository(repository_id)
except httpclient.HTTPError as err:
if err.code == 404:
raise Return(None)
else:
raise
url = repository['data']['service']['location']
api = yield common.service_client('repository', url)
endpoint = api.repository.repositories[repository_id][_pluralise(entity_type)]
raise Return(endpoint)
def get_asset_offers_from_one_repo(repository, data):
repository_id = repository['repository_id']
try:
repo = yield common.get_repository(repository_id)
except httpclient.HTTPError as err:
# Return None if could not get the URL, might be stale index data
if err.code == 404:
raise Return(None)
else:
raise
url = repo['data']['service']['location']
api = yield common.service_client('repository', url)
endpoint = api.repository.repositories[repository_id].search.offers
result = yield common.repository_request(endpoint, repository, data)
raise Return(result)
def get(self):
"""
Retrieve licensors
"""
source_id_type = self.get_argument('source_id_type', None)
source_id = self.get_argument('source_id', None)
if not source_id_type or not source_id:
raise HTTPError(400, 'Must have "source_id_type" and "source_id" parameters')
try:
translated_id = common.translate_id_pair(
{'source_id_type': source_id_type, 'source_id': source_id})
except ValueError:
raise HTTPError(400, '{} is an invalid hub key'.format(source_id))
try:
result = yield licensors.get_asset_licensors(
translated_id['source_id_type'],
translated_id['source_id']
)
self.finish(result)
except httpclient.HTTPError as exc:
body = json.loads(exc.response.body)
raise HTTPError(exc.response.code, body)
def test_post_with_bad_data(service_client, get_repositories, get_repository):
get_repositories.return_value = future_repositories
get_repository.return_value = future_repository
mock_response = Mock()
mock_response.body = '{"errors": [{"source_id_type": "", "message": "not supported asset id type"}]}'
mock_response.code = 400
exc = httpclient.HTTPError(400, response=mock_response)
service_client.return_value = make_future(MagicMock())
client = yield service_client()
endpoint = client.repository.repositories[''].search.offers
endpoint.post.side_effect = exc
handler = _create_offers_handler()
# MUT
handler.request.body = ('[{"source_id":' +
'"https://openpermissions.org/s0/hub1/asset/exampleco/ExampleCoPictureID/1",' +
'"source_id_type":""}]')
with pytest.raises(HTTPError) as excinfo:
handler.post().result()
assert excinfo.value.status_code == mock_response.code
assert excinfo.value.errors == json.loads(mock_response.body)
simple_httpclient.py 文件源码
项目:My-Web-Server-Framework-With-Python2.7
作者: syjsu
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
value = HTTPError(599, "Stream closed")
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def pushMessage2Client(self):
from tornado.httpclient import HTTPClient, HTTPError
from tornado.escape import json_decode
self.logger.info("Print finished.")
http_client = HTTPClient()
#data = {"device_id": self.profile['boxid']}
data = {"device_id": self.profile['boxid']}
(headers, body) = self.createFormData(data)
try:
response = http_client.fetch("http://yun.mohou.com/api/cloud/push-message", method='POST', headers=headers, body=body, request_timeout=10)
data = json_decode(response.body)
self.logger.info("Response result: %s." % str(response.body))
if data['code'] == 0:
return 0
else:
return 1
except HTTPError as err:
self.logger.error("HTTPError: " + str(err))
return 2
except Exception as ex:
self.logger.error("Exception: " + str(ex))
return 2
def test_create_service_404_error(self, _get_service):
client = MagicMock()
organisation_id = 'organisation-id'
name = 'service-name'
location = 'https://example.com'
service_type = 'external'
client.accounts.organisations = {'organisation-id': MagicMock()}
client.accounts.organisations[organisation_id].services.post.side_effect = HTTPError(404, 'Not Found')
with pytest.raises(click.ClickException) as exc:
_create_service(client, organisation_id, name, location, service_type)
client.accounts.organisations[organisation_id].services.post.assert_called_once_with(
name='service-name', location='https://example.com', service_type='external')
assert not _get_service.called
assert exc.value.message == ('\x1b[31mOrganisation organisation-id cannot be found. '
'Please check organisation_id.\x1b[0m')
def test_create_service_already_created(self, _get_service):
client = MagicMock()
organisation_id = 'organisation-id'
name = 'service-name'
location = 'https://example.com'
service_type = 'external'
client.accounts.organisations = {'organisation-id': MagicMock()}
client.accounts.organisations[organisation_id].services.post.side_effect = HTTPError(400, 'Service already Created')
result = _create_service(client, organisation_id, name, location, service_type)
client.accounts.organisations[organisation_id].services.post.assert_called_once_with(
name='service-name', location='https://example.com', service_type='external')
_get_service.assert_called_once_with(client, 'organisation-id', 'service-name')
assert result == 'service-id'
def test_create_service_http_error_no_service(self, _get_service):
client = MagicMock()
organisation_id = 'organisation-id'
name = 'service-name'
location = 'https://example.com'
service_type = 'external'
client.accounts.organisations = {'organisation-id': MagicMock()}
client.accounts.organisations[organisation_id].services.post.side_effect = HTTPError(400, 'Error Message')
with pytest.raises(HTTPError) as exc:
_create_service(client, organisation_id, name, location, service_type)
client.accounts.organisations[organisation_id].services.post.assert_called_once_with(
name='service-name', location='https://example.com', service_type='external')
_get_service.assert_called_once_with(client, 'organisation-id', 'service-name')
assert exc.value.message == 'HTTP 400: Error Message'
def _get_service(client, organisation_id, name):
"""
Get service belonging to organisation which matches given service name
:param client: Accounts Service API Client
:param organisation_id: Id of Organisation
:param name: Service Name
:return: Service Id
"""
try:
response = client.accounts.services.get(organisation_id=organisation_id)
except httpclient.HTTPError as exc:
if exc.code == 404:
# If error is a 404 then this means the organisation_id is not recognised. Raise this error immediately
msg = ('Organisation {} cannot be found. '
'Please check organisation_id.'.format(organisation_id))
raise click.ClickException(click.style(msg, fg='red'))
else:
raise exc
services = [s for s in response['data'] if s['name'] == name]
if services:
return services[0]['id']
return None
def _get_client_secret(client, service_id):
"""
Get client secret for service
:param client: Accounts Service API Client
:param service_id: Service ID
:return: Client secret (if available)
"""
try:
response = client.accounts.services[service_id].secrets.get()
except httpclient.HTTPError as exc:
if exc.code == 404:
# If error is a 404 then this means the service_id is not recognised. Raise this error immediately
msg = ('Service {} cannot be found.'.format(service_id))
raise click.ClickException(click.style(msg, fg='red'))
else:
raise exc
client_secrets = response['data']
if client_secrets:
return client_secrets[0]
return None
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def unregister(self, urlpath):
"""Unregisters a previously registered urlpath.
If the urlpath is not found in the reverse proxy, it will not raise
an error, but it will log the unexpected circumstance.
Parameters
----------
urlpath: str
The absolute path of the url (e.g. /my/internal/service/"
"""
self.log.info("Deregistering {} redirection".format(urlpath))
try:
yield self._reverse_proxy.api_request(urlpath, method='DELETE')
except httpclient.HTTPError as e:
if e.code == 404:
self.log.warning("Could not find urlpath {} when removing"
" container. In any case, the reverse proxy"
" does not map the url. Continuing".format(
urlpath))
else:
raise e
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def _on_timeout(self, key, info=None):
"""Timeout callback of request.
Construct a timeout HTTPResponse when a timeout occurs.
:arg object key: A simple object to mark the request.
:info string key: More detailed timeout information.
"""
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
error_message = "Timeout {0}".format(info) if info else "Timeout"
timeout_response = HTTPResponse(
request, 599, error=HTTPError(599, error_message),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def _on_timeout(self, key, info=None):
"""Timeout callback of request.
Construct a timeout HTTPResponse when a timeout occurs.
:arg object key: A simple object to mark the request.
:info string key: More detailed timeout information.
"""
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
error_message = "Timeout {0}".format(info) if info else "Timeout"
timeout_response = HTTPResponse(
request, 599, error=HTTPError(599, error_message),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
def get_posts_url_from_page(page_url):
"""
????????????URL
:param page_url
:return:
"""
try:
response = yield httpclient.AsyncHTTPClient().fetch(page_url, headers=headers)
soup = BeautifulSoup(response.body, 'html.parser')
posts_tag = soup.find_all('div', class_="post floated-thumb")
urls = []
for index, archive in enumerate(posts_tag):
meta = archive.find("div", class_="post-meta")
url = meta.p.a['href']
urls.append(url)
raise gen.Return(urls)
except httpclient.HTTPError as e:
print('Exception: %s %s' % (e, page_url))
raise gen.Return([])
def execute_oauth_request(self, request, raise_error=True):
"""
Execute an OAuth request
Retry with a new set of tokens when the OAuth access token is expired or rejected
"""
if self.credentials.is_expired():
self.log.info("The OAuth token for %s expired at %s", self.user,
ctime(self.credentials.expires_at))
yield self.refresh_tokens()
self.authorize_request(request)
try:
return (yield self.execute_request(request, raise_error))
except HTTPError as e:
if e.response.code != 401:
raise
# Try once more with a new set of tokens
self.log.info("The OAuth token for %s was rejected", self.user)
yield self.refresh_tokens()
self.authorize_request(request)
return (yield self.execute_request(request, raise_error))
def execute_request(self, request, raise_error=True):
"""
Execute an HTTP request and log the error, if any
"""
self.log.debug("%s %s", request.method, request.url)
http_client = AsyncHTTPClient()
request.headers.update({
'User-Agent': 'jupyterhub-carina/' + __version__
})
try:
return (yield http_client.fetch(request, raise_error=raise_error))
except HTTPError as e:
self.log.exception('An error occurred executing %s %s:\n(%s) %s',
request.method, request.url, e.response.code, e.response.body)
raise