Python tornado.httpclient.HTTPError() Examples

The following are 30 code examples of tornado.httpclient.HTTPError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.httpclient , or try the search function .
Example #1
Source File: run-ui-tests.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def TerminateUser(info_dict, login_resp):
  http_headers = {
    'Cookie': '_xsrf=fake_xsrf; user=%s' % login_resp['cookie'],
    'X-XSRFToken': 'fake_xsrf',
    'Content-Type': 'application/json'
    }
  body = {
    'headers': login_resp['headers'],
    }

  op_id = AllocateIds(['o'], login_resp['cookie'])
  if op_id is not None:
    body['headers']['op_id'] = op_id['asset_ids'][0]

  try:
    response = _CallService('/service/terminate_account', body, http_headers)
  except HTTPError:
    return None;
  else:
    return json.loads(response.body) 
Example #2
Source File: test_app.py    From webssh with MIT License 6 votes vote down vote up
def test_app_auth_with_pubkey_cannot_be_decoded_by_multipart_form(self):
        url = self.get_url('/')
        privatekey = 'h' * 1024
        files = [('privatekey', 'user_rsa_key', privatekey)]
        content_type, body = encode_multipart_formdata(self.body_dict.items(),
                                                       files)
        body = body.encode('utf-8')
        # added some gbk bytes to the privatekey, make it cannot be decoded
        body = body[:-100] + b'\xb4\xed\xce\xf3' + body[-100:]
        headers = {
            'Content-Type': content_type, 'content-length': str(len(body))
        }
        if swallow_http_errors:
            response = yield self.async_post(url, body, headers=headers)
            self.assertIn(b'Invalid unicode', response.body)
        else:
            with self.assertRaises(HTTPError) as ctx:
                yield self.async_post(url, body, headers=headers)
            self.assertIn('Bad Request', ctx.exception.message) 
Example #3
Source File: test_app.py    From webssh with MIT License 6 votes vote down vote up
def test_app_auth_with_pubkey_exceeds_key_max_size(self):
        url = self.get_url('/')
        privatekey = 'h' * (handler.PrivateKey.max_length + 1)
        files = [('privatekey', 'user_rsa_key', privatekey)]
        content_type, body = encode_multipart_formdata(self.body_dict.items(),
                                                       files)
        headers = {
            'Content-Type': content_type, 'content-length': str(len(body))
        }
        if swallow_http_errors:
            response = yield self.async_post(url, body, headers=headers)
            self.assertIn(b'Invalid key', response.body)
        else:
            with self.assertRaises(HTTPError) as ctx:
                yield self.async_post(url, body, headers=headers)
            self.assertIn('Bad Request', ctx.exception.message) 
Example #4
Source File: kancolle.py    From ooi2 with GNU General Public License v2.0 6 votes vote down vote up
def _get_dmm_token(self):
        try:
            req = yield self.http_client.fetch(dmm.LOGIN_URL, headers=self.headers,
                                               connect_timeout=self.connect_timeout,
                                               request_timeout=self.request_timeout,
                                               proxy_host=proxy_host, proxy_port=proxy_port)
        except (CurlError, HTTPError):
            raise OoiAuthError('连接DMM网站失败')

        body = native_str(req.body)
        m = self.dmm_token_pattern.search(body)
        if m:
            dmm_token = m.group(1)
        else:
            raise OoiAuthError('获取DMM token失败')
        m = self.token_pattern.search(body)
        if m:
            token = m.group(1)
        else:
            raise OoiAuthError('获取token失败')

        return dmm_token, token 
Example #5
Source File: kancolle.py    From ooi2 with GNU General Public License v2.0 6 votes vote down vote up
def _get_ajax_token(self, dmm_token, token):
        self.headers.update({'Origin': 'https://www.dmm.com',
                             'Referer': dmm.LOGIN_URL,
                             'Cookie': 'ckcy=1; check_open_login=1; check_down_login=1',
                             'DMM_TOKEN': dmm_token,
                             'X-Requested-With': 'XMLHttpRequest'})
        body = urlencode({'token': token})
        try:
            req = yield self.http_client.fetch(dmm.AJAX_TOKEN_URL, method='POST', headers=self.headers, body=body,
                                               connect_timeout=self.connect_timeout,
                                               request_timeout=self.request_timeout,
                                               proxy_host=proxy_host, proxy_port=proxy_port)
        except (CurlError, HTTPError):
            raise OoiAuthError('DMM网站AJAX请求失败')
        j = json_decode(native_str(req.body))
        return j['token'], j['login_id'], j['password'] 
Example #6
Source File: test_app.py    From webssh with MIT License 6 votes vote down vote up
def test_app_auth_with_invalid_pubkey_for_user_robey(self):
        url = self.get_url('/')
        privatekey = 'h' * 1024
        files = [('privatekey', 'user_rsa_key', privatekey)]
        content_type, body = encode_multipart_formdata(self.body_dict.items(),
                                                       files)
        headers = {
            'Content-Type': content_type, 'content-length': str(len(body))
        }

        if swallow_http_errors:
            response = yield self.async_post(url, body, headers=headers)
            self.assertIn(b'Invalid key', response.body)
        else:
            with self.assertRaises(HTTPError) as ctx:
                yield self.async_post(url, body, headers=headers)
            self.assertIn('Bad Request', ctx.exception.message) 
Example #7
Source File: kancolle.py    From ooi2 with GNU General Public License v2.0 6 votes vote down vote up
def _get_world(self, osapi_url):
        qs = parse_qs(urlparse(osapi_url).query)
        owner = qs['owner'][0]
        self.owner = owner
        st = qs['st'][0]
        url = dmm.GET_WORLD_URL % (owner, int(time.time()*1000))
        self.headers['Referer'] = osapi_url
        try:
            req = yield self.http_client.fetch(url, headers=self.headers,
                                               connect_timeout=self.connect_timeout,
                                               request_timeout=self.request_timeout,
                                               proxy_host=proxy_host, proxy_port=proxy_port)
        except (CurlError, HTTPError):
            raise OoiAuthError('获取服务器ID失败')
        svdata = json_decode(native_str(req.body)[7:])
        if svdata['api_result'] == 1:
            world_id = svdata['api_data']['api_world_id']
        else:
            raise OoiAuthError('服务器ID错误')
        return world_id, st 
Example #8
Source File: simple_httpclient.py    From tornado-zh with MIT License 6 votes vote down vote up
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) 
Example #9
Source File: run-ui-tests.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def AllocateIds(asset_types, user_cookie):
  http_headers = {
    'Cookie': '_xsrf=fake_xsrf; user=%s' % user_cookie,
    'X-XSRFToken': 'fake_xsrf',
    'Content-Type': 'application/json'
    }
  body = {
    'headers': {
      'version': MAX_SUPPORTED_MESSAGE_VERSION
      },
    'asset_types': asset_types
    }
  try:
    response = _CallService('/service/allocate_ids', body, http_headers)
  except HTTPError:
    return None
  else:
    return json.loads(response.body) 
Example #10
Source File: run-ui-tests.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def TerminateUser(info_dict, login_resp):
  http_headers = {
    'Cookie': '_xsrf=fake_xsrf; user=%s' % login_resp['cookie'],
    'X-XSRFToken': 'fake_xsrf',
    'Content-Type': 'application/json'
    }
  body = {
    'headers': login_resp['headers'],
    }

  op_id = AllocateIds(['o'], login_resp['cookie'])
  if op_id is not None:
    body['headers']['op_id'] = op_id['asset_ids'][0]

  try:
    response = _CallService('/service/terminate_account', body, http_headers)
  except HTTPError:
    return None;
  else:
    return json.loads(response.body) 
Example #11
Source File: files.py    From pypi-server with MIT License 6 votes vote down vote up
def proxy_package(cls, package):
        try:
            remote_package_exists = yield PYPIClient.exists(package)

            if remote_package_exists:
                pkg_real_name = yield PYPIClient.find_real_name(package)
                pkg = yield proxy_remote_package(pkg_real_name)

                raise Return(pkg)

            raise LookupError("Remote package not found")
        except (LookupError, HTTPClientError) as e:
            if isinstance(e, HTTPClientError):
                log.warning("PYPI backend return an error: %s", e)

            raise Return(Package.find(package)) 
Example #12
Source File: __init__.py    From tor_access with MIT License 6 votes vote down vote up
def check_access(handler, roleneed):
    
    if isinstance(roleneed,MasterRoleNeed):
        return
    
    if not roleneed:
	raise HTTPError(403)

    checkname = handler.__checkname__
    if handler.__needcheck__.get('url',None):
        if not checkname in roleneed.nodes:
            raise HTTPError(403)

    ctx_params = handler.__needcheck__.get('ctx_param',None)
    if ctx_params:
        for ctx_param in ctx_params.split(','):
            ctx_val = handler.get_argument(ctx_param, handler.path_kwargs.get(ctx_param,None))
            if not ctx_val in roleneed.ctx_vals.get(ctx_param,()):
                raise HTTPError(403) 
Example #13
Source File: simple_httpclient.py    From tornado-zh with MIT License 6 votes vote down vote up
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) 
Example #14
Source File: run-dev.py    From zulip with Apache License 2.0 5 votes vote down vote up
def prepare(self) -> None:
        if 'X-REAL-IP' not in self.request.headers:
            self.request.headers['X-REAL-IP'] = self.request.remote_ip
        if 'X-FORWARDED_PORT' not in self.request.headers:
            self.request.headers['X-FORWARDED-PORT'] = str(proxy_port)
        url = transform_url(
            self.request.protocol,
            self.request.path,
            self.request.query,
            self.target_port,
            self.target_host,
        )
        try:
            fetch_request(
                url=url,
                callback=self.handle_response,
                method=self.request.method,
                headers=self._add_request_headers(["upgrade-insecure-requests"]),
                follow_redirects=False,
                body=getattr(self.request, 'body'),
                allow_nonstandard_methods=True,
            )
        except httpclient.HTTPError as e:
            if hasattr(e, 'response') and e.response:
                self.handle_response(e.response)
            else:
                self.set_status(500)
                self.write('Internal server error:\n' + str(e))
                self.finish() 
Example #15
Source File: run-dev.py    From zulip with Apache License 2.0 5 votes vote down vote up
def handle_response(self, response: Any) -> None:
        if response.error and not isinstance(response.error, httpclient.HTTPError):
            self.set_status(500)
            self.write('Internal server error:\n' + str(response.error))
        else:
            self.set_status(response.code, response.reason)
            self._headers = httputil.HTTPHeaders()  # clear tornado default header

            for header, v in response.headers.get_all():
                # some header appear multiple times, eg 'Set-Cookie'
                self.add_header(header, v)
            if response.body:
                self.write(response.body)
        self.finish() 
Example #16
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_copy(self):
        e = HTTPError(403)
        e2 = copy.copy(e)
        self.assertIsNot(e, e2)
        self.assertEqual(e.code, e2.code) 
Example #17
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_sync_client_error(self):
        # Synchronous HTTPClient raises errors directly; no need for
        # response.rethrow()
        with self.assertRaises(HTTPError) as assertion:
            self.http_client.fetch(self.get_url('/notfound'))
        self.assertEqual(assertion.exception.code, 404) 
Example #18
Source File: httpclient_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_unsupported_auth_mode(self):
        # curl and simple clients handle errors a bit differently; the
        # important thing is that they don't fall back to basic auth
        # on an unknown mode.
        with ExpectLog(gen_log, "uncaught exception", required=False):
            with self.assertRaises((ValueError, HTTPError)):
                self.fetch("/auth", auth_username="Aladdin",
                           auth_password="open sesame",
                           auth_mode="asdf",
                           raise_error=True) 
Example #19
Source File: tornado_requests.py    From keylime with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def request(method,url,params=None,data=None,context=None):

    http_client = httpclient.AsyncHTTPClient()
    if params is not None and len(list(params.keys()))>0:
        url+='?'
        for key in list(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 = await http_client.fetch(request)

    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)
    except ConnectionError as e:
        return tornado_response(599,"Connection error: %s"%e)
    if response is None:
        return None
    return tornado_response(response.code,response.body) 
Example #20
Source File: package.py    From pypi-server with MIT License 5 votes vote down vote up
def get(self, package, version, filename):
        try:
            package = yield PYPIClient.find_real_name(package)
        except (LookupError, HTTPClientError) as e:
            if isinstance(e, HTTPClientError):
                log.warning("PYPI backend return an error: %s", e)

            package = yield self.thread_pool.submit(Package.find, package)

        try:
            pkg_file = yield self.find_file(package, version, filename)
            if not pkg_file.fetched:
                yield self.fetch_remote_file(pkg_file)
        except LookupError:
            self.send_error(404)
        else:
            self.set_header("MD5", pkg_file.md5)
            self.set_header("ETag", pkg_file.md5)
            self.set_header("Content-Length", pkg_file.size)
            self.set_header("Content-Type", 'application/octet-stream')
            self.set_header("Date", pkg_file.ts.strftime("%a, %d %b %Y %H:%M:%S %Z"))

            with pkg_file.open() as f:
                reader = threaded(f.read)

                data = yield reader(self.CHUNK_SIZE)
                self.write(data)
                yield Task(self.flush)

                for chunk in iter(lambda: reader(self.CHUNK_SIZE), None):
                    data = yield chunk
                    if not data:
                        break
                    self.write(data)
                    yield Task(self.flush)

            self.finish() 
Example #21
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_unsupported_auth_mode(self):
        # curl and simple clients handle errors a bit differently; the
        # important thing is that they don't fall back to basic auth
        # on an unknown mode.
        with ExpectLog(gen_log, "uncaught exception", required=False):
            with self.assertRaises((ValueError, HTTPError)):
                response = self.fetch("/auth", auth_username="Aladdin",
                                      auth_password="open sesame",
                                      auth_mode="asdf")
                response.rethrow() 
Example #22
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_invalid_subdomains(self):
        port = self.get_http_port()

        url = 'ws://localhost:%d/echo' % port
        # Subdomains should be disallowed by default.  If we could pass a
        # resolver to websocket_connect we could test sibling domains as well.
        headers = {'Origin': 'http://subtenant.localhost'}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))

        self.assertEqual(cm.exception.code, 403) 
Example #23
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_invalid(self):
        port = self.get_http_port()

        url = 'ws://127.0.0.1:%d/echo' % port
        # Host is 127.0.0.1, which should not be accessible from some other
        # domain
        headers = {'Origin': 'http://somewhereelse.com'}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))

        self.assertEqual(cm.exception.code, 403) 
Example #24
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_check_origin_invalid_partial_url(self):
        port = self.get_http_port()

        url = 'ws://127.0.0.1:%d/echo' % port
        headers = {'Origin': '127.0.0.1:%d' % port}

        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(HTTPRequest(url, headers=headers))
        self.assertEqual(cm.exception.code, 403) 
Example #25
Source File: websocket_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_websocket_http_fail(self):
        with self.assertRaises(HTTPError) as cm:
            yield self.ws_connect('/notfound')
        self.assertEqual(cm.exception.code, 404) 
Example #26
Source File: email_client.py    From Doodle with MIT License 5 votes vote down vote up
def send_email(to, subject, html):
    if isinstance(to, unicode):
        to = to.encode('utf-8')
    if isinstance(subject, unicode):
        subject = subject.encode('utf-8')
    if isinstance(html, unicode):
        html = html.encode('utf-8')

    data = {
        'from': CONFIG.EMAIL_SENDER,
        'to': to,
        'subject': subject,
        'html': html
    }
    data = urlencode(data)
    request = HTTPRequest(
        url=_MAILGUN_API_URL,
        method='POST',
        auth_username='api',
        auth_password=CONFIG.MAILGUN_API_KEY,
        body=data
    )
    client = AsyncHTTPClient()
    try:
        yield client.fetch(request)
    except HTTPError as e:
        try:
            response = e.response.body
        except AttributeError:
            response = None
        logging.exception('failed to send email:\nto: %s\nsubject: %s\nhtml: %s\nresponse: %s', to, subject, html, response) 
Example #27
Source File: run-ui-tests.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def LoginUser(info_dict):
  http_headers = {
    'Content-Type': 'application/json',
    'Cookie': '_xsrf=fake_xsrf',
    'X-XSRFToken': 'fake_xsrf',
    }
  body = {
    'headers': {
      'version': MAX_SUPPORTED_MESSAGE_VERSION
      },
    #'cookie_in_response': True,
    'auth_info': info_dict
    }
  try:
    response = _CallService('/login/fakeviewfinder', body, http_headers)
  except HTTPError:
    return None
  else:
    # Parse out user cookie and store in response.body as 'cookie'.
    user_cookie = GetCookieFromResponse(response)
    if user_cookie is None:
      raise "An error occurred getting user cookie from response."

    # Add user cookie to reponse.body.
    json_response = json.loads(response.body)
    json_response['cookie'] = user_cookie

    return json_response 
Example #28
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_sync_client_error(self):
        # Synchronous HTTPClient raises errors directly; no need for
        # response.rethrow()
        with self.assertRaises(HTTPError) as assertion:
            self.http_client.fetch(self.get_url('/notfound'))
        self.assertEqual(assertion.exception.code, 404) 
Example #29
Source File: httpclient_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_future_http_error(self):
        try:
            yield self.http_client.fetch(self.get_url('/notfound'))
        except HTTPError as e:
            self.assertEqual(e.code, 404)
            self.assertEqual(e.response.code, 404) 
Example #30
Source File: websocket_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_websocket_http_fail(self):
        with self.assertRaises(HTTPError) as cm:
            yield websocket_connect(
                'ws://localhost:%d/notfound' % self.get_http_port(),
                io_loop=self.io_loop)
        self.assertEqual(cm.exception.code, 404)