本文整理汇总了Python中requests.models.Response.connection方法的典型用法代码示例。如果您正苦于以下问题:Python Response.connection方法的具体用法?Python Response.connection怎么用?Python Response.connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.models.Response
的用法示例。
在下文中一共展示了Response.connection方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _receive_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def _receive_response(self, task, response):
"""
Called by the delegate when a response has been received.
This call is expected only on background threads, and thus may not do
anything that is not Python-thread-safe. This means that, for example,
it is safe to grab things from the _tasks dictionary, but it is not
safe to make other method calls on this object unless they explicitly
state that they are safe in background threads.
"""
queue, request = self._tasks[task]
resp = Response()
resp.status_code = getKey(response, 'statusCode')
resp.reason = ''
# TODO: Why do I have to do this?
raw_headers = getKey(response, 'allHeaderFields')
resp.headers = CaseInsensitiveDict(raw_headers)
resp.encoding = get_encoding_from_headers(resp.headers)
# TODO: This needs to point to an object that we can use to provide
# the various raw things that requests needs.
resp.raw = None
if isinstance(request.url, bytes):
resp.url = request.url.decode('utf-8')
else:
resp.url = request.url
resp.request = request
resp.connection = self
# Put this response on the queue.
queue.put_nowait(resp)
示例2: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def build_response(self, request, resp):
"""
Builds a Requests' response object. This emulates most of the logic of
the standard fuction but deals with the lack of the ``.headers``
property on the HTTP20Response object.
"""
response = Response()
response.status_code = resp.status
response.headers = CaseInsensitiveDict(resp.getheaders())
response.raw = resp
response.reason = resp.reason
response.encoding = get_encoding_from_headers(response.headers)
extract_cookies_to_jar(response.cookies, request, response)
if isinstance(request.url, bytes):
response.url = request.url.decode('utf-8')
else:
response.url = request.url
response.request = request
response.connection = self
# One last horrible patch: Requests expects its raw responses to have a
# release_conn method, which I don't. We should monkeypatch a no-op on.
resp.release_conn = lambda: None
return response
示例3: send
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def send(self, request, **kwargs):
url = urlparse(request.url)
if url.scheme != 'https':
raise Exception('Only HTTPS is supported!')
ctx = self._make_context()
conn = httpslib.HTTPSConnection(
url.hostname, url.port or 443, ssl_context=ctx)
conn.request(request.method, url.path, request.body, request.headers)
resp = conn.getresponse()
response = Response()
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(resp, 'status', None)
# Make headers case-insensitive.
response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
# Set encoding.
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response.reason = response.raw.reason
if isinstance(request.url, bytes):
response.url = request.url.decode('utf-8')
else:
response.url = request.url
# Give the Response some context.
response.request = request
response.connection = self
return response
示例4: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def build_response(self, req, resp):
"""Builds a :class:`Response <requests.Response>` object from a urllib3
response. This should not be called from user code, and is only exposed
for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
:param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
:param resp: The urllib3 response object.
"""
response = Response()
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(resp, 'status', None)
# Make headers case-insensitive.
response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
# Set encoding.
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response.reason = response.raw.reason
if isinstance(req.url, bytes):
response.url = req.url.decode('utf-8')
else:
response.url = req.url
# Add new cookies from the server.
extract_cookies_to_jar(response.cookies, req, resp)
# Give the Response some context.
response.request = req
response.connection = self
return response
示例5: send
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def send(self, request, **kwargs):
response = Response()
response.headers = {}
response.encoding = 'utf-8' # FIXME: this is a complete guess
response.url = request.url
response.request = request
response.connection = self
try:
response.raw = open(request.url.replace('file://', ''), 'r')
except IOError as e:
response.status_code = 404
return response
response.status_code = 200
return response
示例6: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import connection [as 别名]
def build_response(self, request, resp):
"""
Builds a Requests' response object. This emulates most of the logic of
the standard fuction but deals with the lack of the ``.headers``
property on the HTTP20Response object.
Additionally, this function builds in a number of features that are
purely for HTTPie. This is to allow maximum compatibility with what
urllib3 does, so that HTTPie doesn't fall over when it uses us.
"""
response = Response()
response.status_code = resp.status
response.headers = CaseInsensitiveDict(resp.headers.iter_raw())
response.raw = resp
response.reason = resp.reason
response.encoding = get_encoding_from_headers(response.headers)
extract_cookies_to_jar(response.cookies, request, response)
response.url = request.url
response.request = request
response.connection = self
# First horrible patch: Requests expects its raw responses to have a
# release_conn method, which I don't. We should monkeypatch a no-op on.
resp.release_conn = lambda: None
# Next, add the things HTTPie needs. It needs the following things:
#
# - The `raw` object has a property called `_original_response` that is
# a `httplib` response object.
# - `raw._original_response` has three simple properties: `version`,
# `status`, `reason`.
# - `raw._original_response.version` has one of three values: `9`,
# `10`, `11`.
# - `raw._original_response.msg` exists.
# - `raw._original_response.msg._headers` exists and is an iterable of
# two-tuples.
#
# We fake this out. Most of this exists on our response object already,
# and the rest can be faked.
#
# All of this exists for httpie, which I don't have any tests for,
# so I'm not going to bother adding test coverage for it.
class FakeOriginalResponse(object): # pragma: no cover
def __init__(self, headers):
self._headers = headers
def get_all(self, name, default=None):
values = []
for n, v in self._headers:
if n == name.lower():
values.append(v)
if not values:
return default
return values
def getheaders(self, name):
return self.get_all(name, [])
response.raw._original_response = orig = FakeOriginalResponse(None)
orig.version = 20
orig.status = resp.status
orig.reason = resp.reason
orig.msg = FakeOriginalResponse(resp.headers.iter_raw())
return response