python类Response()的实例源码

http.py 文件源码 项目:oscars2016 作者: 0x0ece 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
http.py 文件源码 项目:sndlatr 作者: Schibum 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = simplejson.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    return httplib2.Response(resp), content
http.py 文件源码 项目:GAMADV-XTD 作者: taers232c 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:GAMADV-XTD 作者: taers232c 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
test_requests_responses.py 文件源码 项目:yahoo-fantasy-football-metrics 作者: uberfastman 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def request(self, uri, method="GET", body=None, headers=None, redirections=5):
        path = urlparse.urlparse(uri)[2]
        fname = os.path.join(HTTP_SRC_DIR, path[1:])

        if not os.path.exists(fname):
            index = self.hit_counter.get(fname, 1)

            if os.path.exists(fname + "." + str(index)):
                self.hit_counter[fname] = index + 1
                fname = fname + "." + str(index)

        if os.path.exists(fname):
            f = file(fname, "r")
            response = message_from_file(f)
            f.close()
            body = response.get_payload()
            response_headers = httplib2.Response(response)
            return (response_headers, body)
        else:
            return (httplib2.Response({"status": "404"}), "")
http.py 文件源码 项目:office-interoperability-tools 作者: milossramek 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:office-interoperability-tools 作者: milossramek 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = simplejson.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    return httplib2.Response(resp), content
test_requests_responses.py 文件源码 项目:Taigabot 作者: FrozenPigs 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def request(self, uri, method="GET", body=None, headers=None, redirections=5):
        path = urlparse.urlparse(uri)[2]
        fname = os.path.join(HTTP_SRC_DIR, path[1:])

        if not os.path.exists(fname):
            index = self.hit_counter.get(fname, 1)

            if os.path.exists(fname + "." + str(index)):
                self.hit_counter[fname] = index + 1
                fname = fname + "." + str(index)

        if os.path.exists(fname):
            f = file(fname, "r")
            response = message_from_file(f)
            f.close()
            body = response.get_payload()
            response_headers = httplib2.Response(response)
            return (response_headers, body)
        else:
            return (httplib2.Response({"status": "404"}), "")
test_gce.py 文件源码 项目:omni 作者: openstack 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_delete_sg_rule_with_error(self, mock_delete, mock_wait):
        http_error = gce_errors.HttpError(
            httplib2.Response({
                'status': 404,
                'reason': 'Not Found'
            }),
            content='')
        mock_delete.side_effect = http_error
        mock_wait.side_effect = gce_mock.wait_for_operation
        sg_rule = self.get_fake_sg_rule()
        self.assertIsNone(
            self._driver._delete_secgrp_rule(self.context, sg_rule['id']))
        mock_delete.assert_called_once_with(self._driver.gce_svc,
                                            self._driver.gce_project,
                                            "secgrp-" + sg_rule['id'])
        mock_wait.assert_not_called()
tests.py 文件源码 项目:socialauth 作者: emilyhorsman 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def mock_valid_requests(self, uri, *args, **kwargs):
    url = urlparse(uri)

    # https://dev.twitter.com/oauth/reference/post/oauth/request_token
    if url[1] == 'api.twitter.com' and url[2] == '/oauth/request_token':
        res = httplib2.Response(dict(status = 200))
        content = b'oauth_token=foo&oauth_token_secret=bar&oauth_callback_confirmed=true'

    # https://dev.twitter.com/oauth/reference/post/oauth/access_token
    if url[1] == 'api.twitter.com' and url[2] == '/oauth/access_token':
        res = httplib2.Response(dict(status = 200))
        content = b'oauth_token=foo&oauth_token_secret=bar&user_id=987&screen_name=test'

    # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#confirm
    if url[1] == 'graph.facebook.com' and url[2].endswith('/oauth/access_token'):
        res = httplib2.Response(dict(status = 200))
        content = b'{"access_token":"foobar","token_type":"bearer","expires_in":5117097}'

    # https://developers.facebook.com/docs/graph-api/using-graph-api
    if url[1] == 'graph.facebook.com' and url[2] == '/me':
        res = httplib2.Response(dict(status = 200))
        content = b'{"id":"987","name":"test"}'

    return (res, content)
test_mlengine_operator.py 文件源码 项目:incubator-airflow-old 作者: apache 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def testHttpError(self):
        http_error_code = 403
        with patch('airflow.contrib.operators.mlengine_operator.MLEngineHook') \
                as mock_hook:
            hook_instance = mock_hook.return_value
            hook_instance.create_job.side_effect = errors.HttpError(
                resp=httplib2.Response({
                    'status': http_error_code
                }), content=b'Forbidden')

            with self.assertRaises(errors.HttpError) as context:
                training_op = MLEngineTrainingOperator(
                    **self.TRAINING_DEFAULT_ARGS)
                training_op.execute(None)

            mock_hook.assert_called_with(
                gcp_conn_id='google_cloud_default', delegate_to=None)
            # Make sure only 'create_job' is invoked on hook instance
            self.assertEquals(len(hook_instance.mock_calls), 1)
            hook_instance.create_job.assert_called_with(
                'test-project', self.TRAINING_INPUT, ANY)
            self.assertEquals(http_error_code, context.exception.resp.status)
http.py 文件源码 项目:REMAP 作者: REMAPApp 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:REMAP 作者: REMAPApp 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
http.py 文件源码 项目:OneClickDTU 作者: satwikkansal 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:OneClickDTU 作者: satwikkansal 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
httplib2_test.py 文件源码 项目:httplib2shim 作者: GoogleCloudPlatform 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testDigestObjectStale(self):
        credentials = ('joe', 'password')
        host = None
        request_uri = '/projects/httplib2/test/digest/'
        headers = {}
        response = httplib2.Response({})
        response['www-authenticate'] = (
            'Digest realm="myrealm", '
            'nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", '
            'algorithm=MD5, qop="auth", stale=true')
        response.status = 401
        content = b""
        d = httplib2.DigestAuthentication(
            credentials, host, request_uri, headers, response, content, None)
        # Returns true to force a retry
        self.assertTrue(d.response(response, content))
httplib2_test.py 文件源码 项目:httplib2shim 作者: GoogleCloudPlatform 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def testDigestObjectAuthInfo(self):
        credentials = ('joe', 'password')
        host = None
        request_uri = '/projects/httplib2/test/digest/'
        headers = {}
        response = httplib2.Response({})
        response['www-authenticate'] = (
            'Digest realm="myrealm", '
            'nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", '
            'algorithm=MD5, qop="auth", stale=true')
        response['authentication-info'] = 'nextnonce="fred"'
        content = b""
        d = httplib2.DigestAuthentication(
            credentials, host, request_uri, headers, response, content, None)
        # Returns true to force a retry
        self.assertFalse(d.response(response, content))
        self.assertEqual('fred', d.challenge['nonce'])
        self.assertEqual(1, d.challenge['nc'])
__init__.py 文件源码 项目:httplib2shim 作者: GoogleCloudPlatform 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _map_response(response, decode=False):
    """Maps a urllib3 response to a httplib/httplib2 Response."""
    # This causes weird deepcopy errors, so it's commented out for now.
    # item._urllib3_response = response
    item = httplib2.Response(response.getheaders())
    item.status = response.status
    item['status'] = str(item.status)
    item.reason = response.reason
    item.version = response.version

    # httplib2 expects the content-encoding header to be stripped and the
    # content length to be the length of the uncompressed content.
    # This does not occur for 'HEAD' requests.
    if decode and item.get('content-encoding') in ['gzip', 'deflate']:
        item['content-length'] = str(len(response.data))
        item['-content-encoding'] = item.pop('content-encoding')

    return item
http.py 文件源码 项目:alfredToday 作者: jeeftor 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
http.py 文件源码 项目:Webradio_v2 作者: Acer54 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:Webradio_v2 作者: Acer54 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
http.py 文件源码 项目:GAMADV-X 作者: taers232c 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:GAMADV-X 作者: taers232c 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
test_boot_resources_create.py 文件源码 项目:maas 作者: maas 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_put_upload_sends_content_type_and_length_headers(self):
        response = httplib2.Response({'status': 200})
        mock_request = self.patch(boot_resources_create, 'http_request')
        mock_request.return_value = (response, b'')
        action = self.make_boot_resources_create_action()
        self.patch(action, 'sign')
        data = factory.make_bytes()
        action.put_upload(sentinel.upload_uri, data)
        headers = {
            'Content-Type': 'application/octet-stream',
            'Content-Length': '%s' % len(data),
            }
        self.assertThat(
            mock_request,
            MockCalledOnceWith(
                sentinel.upload_uri, 'PUT', body=ANY,
                headers=headers, insecure=False))
test_utils.py 文件源码 项目:maas 作者: maas 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test__prints_textual_response_with_success_msg(self):
        # When the response has a status code of 2XX, and the response body is
        # textual print_response_content() will print a success message to the
        # TTY.
        status_code = random.randrange(200, 300)
        response = httplib2.Response({
            'status': status_code,
            'content': b"Lorem ipsum dolor sit amet.",
            'content-type': 'text/unicode',
            })
        buf = io.BytesIO()
        self.patch(buf, 'isatty').return_value = True
        utils.print_response_content(response, response['content'], buf)
        self.assertEqual(
            b"Success.\n"
            b"Machine-readable output follows:\n" +
            response['content'] + b"\n", buf.getvalue())
test_api.py 文件源码 项目:maas 作者: maas 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_fetch_api_description_not_okay(self):
        # If the response is not 200 OK, fetch_api_description throws toys.
        content = factory.make_name("content")
        request = self.patch(httplib2.Http, "request")
        response = httplib2.Response({})
        response.status = http.client.BAD_REQUEST
        response.reason = http.client.responses[http.client.BAD_REQUEST]
        request.return_value = response, json.dumps(content)
        error = self.assertRaises(
            CommandError, api.fetch_api_description,
            "http://example.com/api/2.0/")
        error_expected = "%d %s:\n%s" % (
            http.client.BAD_REQUEST,
            http.client.responses[http.client.BAD_REQUEST],
            json.dumps(content))
        self.assertEqual(error_expected, "%s" % error)
http.py 文件源码 项目:share-class 作者: junyiacademy 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self, resp, content, postproc):
    """Constructor for HttpRequestMock

    Args:
      resp: httplib2.Response, the response to emulate coming from the request
      content: string, the response body
      postproc: callable, the post processing function usually supplied by
                the model class. See model.JsonModel.response() as an example.
    """
    self.resp = resp
    self.content = content
    self.postproc = postproc
    if resp is None:
      self.resp = httplib2.Response({'status': 200, 'reason': 'OK'})
    if 'reason' in self.resp:
      self.resp.reason = self.resp['reason']
http.py 文件源码 项目:share-class 作者: junyiacademy 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def request(self, uri,
              method='GET',
              body=None,
              headers=None,
              redirections=1,
              connection_type=None):
    resp, content = self._iterable.pop(0)
    if content == 'echo_request_headers':
      content = headers
    elif content == 'echo_request_headers_as_json':
      content = json.dumps(headers)
    elif content == 'echo_request_body':
      if hasattr(body, 'read'):
        content = body.read()
      else:
        content = body
    elif content == 'echo_request_uri':
      content = uri
    if isinstance(content, six.text_type):
      content = content.encode('utf-8')
    return httplib2.Response(resp), content
fake_http.py 文件源码 项目:LIS-Tempest 作者: LIS 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def request(self, uri, method="GET", body=None, headers=None,
                redirections=5, connection_type=None):
        if not self.return_type:
            fake_headers = httplib2.Response(headers)
            return_obj = {
                'uri': uri,
                'method': method,
                'body': body,
                'headers': headers
            }
            return (fake_headers, return_obj)
           # return (headers, return_obj)
        elif isinstance(self.return_type, int):
            body = "fake_body"
            header_info = {
                'content-type': 'text/plain',
                'status': str(self.return_type),
                'content-length': len(body)
            }
            resp_header = httplib2.Response(header_info)
            return (resp_header, body)
        else:
            msg = "unsupported return type %s" % self.return_type
            raise TypeError(msg)
http.py 文件源码 项目:oscars2016 作者: 0x0ece 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
                   **kwargs):
  """Retries an HTTP request multiple times while handling errors.

  If after all retries the request still fails, last error is either returned as
  return value (for HTTP 5xx errors) or thrown (for ssl.SSLError).

  Args:
    http: Http object to be used to execute request.
    num_retries: Maximum number of retries.
    req_type: Type of the request (used for logging retries).
    sleep, rand: Functions to sleep for random time between retries.
    uri: URI to be requested.
    method: HTTP method to be used.
    args, kwargs: Additional arguments passed to http.request.

  Returns:
    resp, content - Response from the http request (may be HTTP 5xx).
  """
  resp = None
  for retry_num in range(num_retries + 1):
    if retry_num > 0:
      sleep(rand() * 2**retry_num)
      logging.warning(
          'Retry #%d for %s: %s %s%s' % (retry_num, req_type, method, uri,
          ', following status: %d' % resp.status if resp else ''))

    try:
      resp, content = http.request(uri, method, *args, **kwargs)
    except ssl.SSLError:
      if retry_num == num_retries:
        raise
      else:
        continue
    if resp.status < 500:
      break

  return resp, content


问题


面经


文章

微信
公众号

扫码关注公众号