Here are the examples of the python api requests.packages.urllib3.response.HTTPResponse taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
4 Examples
0
Example 1
Project: inspectors-general Source File: utils.py
def build_response(self, req, resp):
domain = urllib.parse.urlparse(req.url)[1].split(':')[0]
base_domain = ".".join(domain.split(".")[-2:])
if base_domain in self.SOFT_404_BODY_SIGNATURES:
if resp.getheader("Content-Type") in ["text/html; charset=utf-8",
"text/html"]:
data = resp.data
headers = resp.headers
if resp.getheader("Content-Encoding") == "gzip":
decompressed_data = gzip.decompress(data)
else:
decompressed_data = data
if resp.getheader("Transfer-Encoding") == "chunked":
headers.pop("Transfer-Encoding")
body = io.BytesIO(data)
resp = requests.packages.urllib3.response.HTTPResponse(
body=body,
headers=headers,
status=resp.status,
version=resp.version,
reason=resp.reason,
strict=resp.strict,
preload_content=False,
)
if decompressed_data.find(self.SOFT_404_BODY_SIGNATURES[base_domain], 0, 10240) != -1:
result = super(Soft404HttpAdapter, self).build_response(req, resp)
result.status_code = 404 # tells scrapelib to not retry
return result
redirect = resp.get_redirect_location()
result = super(Soft404HttpAdapter, self).build_response(req, resp)
if redirect and self.SOFT_404_URLS_RE.match(redirect):
result.status_code = 404 # tells scrapelib to not retry
return result
0
Example 2
Project: robobrowser Source File: responses.py
def _on_request(self, request, **kwargs):
match = self._find_match(request)
# TODO(dcramer): find the correct class for this
if match is None:
error_msg = 'Connection refused: {0}'.format(request.url)
response = ConnectionError(error_msg)
self._calls.add(request, response)
raise response
headers = {
'Content-Type': match['content_type'],
}
if match['adding_headers']:
headers.update(match['adding_headers'])
response = HTTPResponse(
status=match['status'],
body=BufferIO(match['body']),
headers=headers,
preload_content=False,
)
adapter = HTTPAdapter()
response = adapter.build_response(request, response)
if not match['stream']:
response.content # NOQA
self._calls.add(request, response)
return response
0
Example 3
Project: fjord Source File: response.py
def create_response(request, **kwargs):
"""
:param int status_code: The status code to return upon a successful
match. Defaults to 200.
:param HTTPResponse raw: A HTTPResponse object to return upon a
successful match.
:param io.IOBase body: An IO object with a read() method that can
return a body on successful match.
:param bytes content: A byte string to return upon a successful match.
:param unicode text: A text string to return upon a successful match.
:param object json: A python object to be converted to a JSON string
and returned upon a successful match.
:param dict headers: A dictionary object containing headers that are
returned upon a successful match.
"""
connection = kwargs.pop('connection', _FakeConnection())
_check_body_arguments(**kwargs)
raw = kwargs.pop('raw', None)
body = kwargs.pop('body', None)
content = kwargs.pop('content', None)
text = kwargs.pop('text', None)
json = kwargs.pop('json', None)
encoding = None
if content and not isinstance(content, six.binary_type):
raise TypeError('Content should be a callback or binary data')
if text and not isinstance(text, six.string_types):
raise TypeError('Text should be a callback or string data')
if json is not None:
text = jsonutils.dumps(json)
if text is not None:
encoding = 'utf-8'
content = text.encode(encoding)
if content is not None:
body = six.BytesIO(content)
if not raw:
raw = HTTPResponse(status=kwargs.get('status_code', _DEFAULT_STATUS),
headers=kwargs.get('headers', {}),
reason=kwargs.get('reason'),
body=body or six.BytesIO(six.b('')),
decode_content=False,
preload_content=False,
original_response=compat._fake_http_response)
response = _http_adapter.build_response(request, raw)
response.connection = connection
response.encoding = encoding
return response
0
Example 4
Project: requests-mock Source File: response.py
def create_response(request, **kwargs):
"""
:param int status_code: The status code to return upon a successful
match. Defaults to 200.
:param HTTPResponse raw: A HTTPResponse object to return upon a
successful match.
:param io.IOBase body: An IO object with a read() method that can
return a body on successful match.
:param bytes content: A byte string to return upon a successful match.
:param unicode text: A text string to return upon a successful match.
:param object json: A python object to be converted to a JSON string
and returned upon a successful match.
:param dict headers: A dictionary object containing headers that are
returned upon a successful match.
:param CookieJar cookies: A cookie jar with cookies to set on the
response.
"""
connection = kwargs.pop('connection', _FakeConnection())
_check_body_arguments(**kwargs)
raw = kwargs.pop('raw', None)
body = kwargs.pop('body', None)
content = kwargs.pop('content', None)
text = kwargs.pop('text', None)
json = kwargs.pop('json', None)
encoding = None
if content and not isinstance(content, six.binary_type):
raise TypeError('Content should be binary data')
if text and not isinstance(text, six.string_types):
raise TypeError('Text should be string data')
if json is not None:
text = jsonutils.dumps(json)
if text is not None:
encoding = 'utf-8'
content = text.encode(encoding)
if content is not None:
body = _IOReader(content)
if not raw:
raw = HTTPResponse(status=kwargs.get('status_code', _DEFAULT_STATUS),
headers=kwargs.get('headers', {}),
reason=kwargs.get('reason'),
body=body or _IOReader(six.b('')),
decode_content=False,
preload_content=False,
original_response=compat._fake_http_response)
response = _http_adapter.build_response(request, raw)
response.connection = connection
response.encoding = encoding
_extract_cookies(request, response, kwargs.get('cookies'))
return response