本文整理汇总了Python中requests.models.Response.url方法的典型用法代码示例。如果您正苦于以下问题:Python Response.url方法的具体用法?Python Response.url怎么用?Python Response.url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.models.Response
的用法示例。
在下文中一共展示了Response.url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def request(method, url, **kwargs):
if 'data' in kwargs:
kwargs['params'] = kwargs.pop('data')
elif 'params' in kwargs and kwargs['params'] is None:
kwargs.pop('params')
auth = None
if 'auth' in kwargs:
auth = kwargs.pop('auth')
for i in ['auth', 'allow_redirects', 'stream']:
if i in kwargs:
kwargs.pop(i)
if app.app.registry.api_url in url:
if auth:
authorization = api.authorization
api.authorization = ('Basic', auth)
resp = api._gen_request(method.upper(), url, expect_errors=True, **kwargs)
if auth:
api.authorization = authorization
else:
resp = app._gen_request(method.upper(), url, expect_errors=True, **kwargs)
response = Response()
response.status_code = resp.status_int
response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response._content = resp.body
response.reason = resp.status
if isinstance(url, bytes):
response.url = url.decode('utf-8')
else:
response.url = url
response.request = resp.request
return response
示例2: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [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
示例3: send
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [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: _receive_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [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)
示例5: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [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
示例6: test_disable_default_redirect_cache
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_disable_default_redirect_cache(self, mock_request):
"""
Test disable default redirect cache (by setting default redirect cache to None)
"""
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_here',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_here'
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Vary': 'Accept',
}
response1.history = [response0]
mock_request.return_value = response1
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
set_default_redirect_cache(None)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
示例7: test_get_301_circular_redirect
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_get_301_circular_redirect(self, mock_request):
response0 = Response()
response0.url = 'http://www.test.com/path0'
response0.status_code = 301
response0.headers = {'Location': 'http://www.test.com/path1'}
response1 = Response()
response1.url = 'http://www.test.com/path1'
response1.status_code = 301
response1.headers = {'Location': 'http://www.test.com/path0'}
response2 = Response()
response2.url = 'http://www.test.com/path2'
response2.status_code = 200
response2._content = 'Mocked response content'
response2.history = [response0, response1]
mock_request.return_value = response2
r = get('http://www.test.com/path0')
self.assertEqual(mock_request.call_count, 1)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, 'Mocked response content')
with self.assertRaises(TooManyRedirects):
get('http://www.test.com/path0')
示例8: test_get_301_only_once
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_get_301_only_once(self, mock_request):
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_here',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_here'
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Vary': 'Accept',
}
response1.history = [response0]
mock_request.return_value = response1
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_request.call_count, 1)
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
#assert we not make request to 301 again
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_request.call_count, 2)
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
self.assertEqual(r.status_code, 200)
示例9: test_get_301_thrice
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_get_301_thrice(self, mock_get):
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_1',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_1'
response1.status_code = 301
response1.headers = {
'Location': 'http://www.test.com/redirect_2',
}
response2 = Response()
response2.url = 'http://www.test.com/redirect_2'
response2.status_code = 301
response2.headers = {
'Location': 'http://www.test.com/redirect_3',
}
response3 = Response()
response3.url = 'http://www.test.com/redirect_3'
response3.status_code = 200
response3._content = 'Mocked response content'
response3.headers = {
'Vary': 'Accept',
}
response3.history = [response0, response1, response2]
mock_get.return_value = response3
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_get.call_count, 1)
mock_get.assert_called_with('http://www.test.com/neverseemeagain')
self.assertEqual(r.status_code, 200)
#assert we not make request to 301 again
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_get.call_count, 2)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_1')
self.assertEqual(mock_get.call_count, 3)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_2')
self.assertEqual(mock_get.call_count, 4)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_3')
self.assertEqual(mock_get.call_count, 5)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
示例10: test_domain_cookie
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_domain_cookie(self, mock_request):
"""
Test domain cookies without 'Path'
"""
response0 = Response()
response0.status_code = 200
response0._content = 'Mocked response content'
response0.headers = {
'Set-Cookie': 'a=apple; Domain=fruits.com;, ' +
'b=banana; Domain=fruits.com;, ' +
'c=citrus; Domain=mediterranean.fruits.com;, ' +
'm=mango; Domain=tropical.fruits.com;'
}
response0.url = 'http://mediterranean.fruits.com/path0'
mock_request.return_value = response0
get('http://mediterranean.fruits.com/path0') # Initial request. No cookies.
mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path0', allow_redirects=True)
get('http://mediterranean.fruits.com/path1') # 'a', 'b', and 'c' cookies should be present.
mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path1', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'c': 'citrus'})
get('http://tropical.fruits.com/path2') # 'a', 'b', and 'm' cookies should be present.
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path2', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})
get('http://www.fruits.com/path3') # 'a' and 'b' cookies should be present.
mock_request.assert_called_with('GET', 'http://www.fruits.com/path3', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana'})
get('http://fruits.com/path4') # 'a' and 'b' cookies should be present.
mock_request.assert_called_with('GET', 'http://fruits.com/path4', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana'})
get('http://animals.com/path5') # Different domain. No cookies should be present.
mock_request.assert_called_with('GET', 'http://animals.com/path5', allow_redirects=True)
response1 = Response()
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Set-Cookie': 'a=apricot; Domain=fruits.com;, ' +
'b=; Domain=fruits.com;, ' +
'm=melon; Domain=tropical.fruits.com;'
}
response1.url = 'http://tropical.fruits.com/path0'
mock_request.return_value = response1
get('http://tropical.fruits.com/path0') # Still called with previous cookies
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path0', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})
get('http://tropical.fruits.com/path1') # called with new cookies
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path1', allow_redirects=True,
cookies={'a': 'apricot', 'b': '', 'm': 'melon'})
示例11: setUp
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def setUp(self):
self.cassette = cassette.Cassette(
TestCassette.cassette_name,
'json',
'w+'
)
r = Response()
r.status_code = 200
r.encoding = 'utf-8'
r.headers = CaseInsensitiveDict({'Content-Type': decode('foo')})
r.url = 'http://example.com'
cassette.add_urllib3_response({
'body': {
'string': decode('foo'),
'encoding': 'utf-8'
}
}, r)
self.response = r
r = Request()
r.method = 'GET'
r.url = 'http://example.com'
r.headers = {}
r.data = {'key': 'value'}
self.response.request = r.prepare()
self.response.request.headers.update(
{'User-Agent': 'betamax/test header'}
)
self.json = {
'request': {
'body': 'key=value',
'headers': {
'User-Agent': 'betamax/test header',
'Content-Length': '9',
'Content-Type': 'application/x-www-form-urlencoded',
},
'method': 'GET',
'uri': 'http://example.com/',
},
'response': {
'body': {
'string': decode('foo'),
'encoding': 'utf-8',
},
'headers': {'Content-Type': decode('foo')},
'status_code': 200,
'url': 'http://example.com',
},
'recorded_at': '2013-08-31T00:00:00',
}
self.date = datetime(2013, 8, 31)
self.cassette.save_interaction(self.response, self.response.request)
self.interaction = self.cassette.interactions[0]
self.interaction.recorded_at = self.date
示例12: setUp
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def setUp(self):
# Make a new serializer to test with
self.test_serializer = Serializer()
serializers.serializer_registry["test"] = self.test_serializer
# Instantiate the cassette to test with
self.cassette = cassette.Cassette(TestCassette.cassette_name, "test", record_mode="once")
# Create a new object to serialize
r = Response()
r.status_code = 200
r.reason = "OK"
r.encoding = "utf-8"
r.headers = CaseInsensitiveDict({"Content-Type": decode("foo")})
r.url = "http://example.com"
util.add_urllib3_response({"body": {"string": decode("foo"), "encoding": "utf-8"}}, r)
self.response = r
# Create an associated request
r = Request()
r.method = "GET"
r.url = "http://example.com"
r.headers = {}
r.data = {"key": "value"}
self.response.request = r.prepare()
self.response.request.headers.update({"User-Agent": "betamax/test header"})
# Expected serialized cassette data.
self.json = {
"request": {
"body": {"encoding": "utf-8", "string": "key=value"},
"headers": {
"User-Agent": ["betamax/test header"],
"Content-Length": ["9"],
"Content-Type": ["application/x-www-form-urlencoded"],
},
"method": "GET",
"uri": "http://example.com/",
},
"response": {
"body": {"string": decode("foo"), "encoding": "utf-8"},
"headers": {"Content-Type": [decode("foo")]},
"status": {"code": 200, "message": "OK"},
"url": "http://example.com",
},
"recorded_at": "2013-08-31T00:00:00",
}
self.date = datetime(2013, 8, 31)
self.cassette.save_interaction(self.response, self.response.request)
self.interaction = self.cassette.interactions[0]
self.interaction.recorded_at = self.date
示例13: test_redirect
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def test_redirect(self, mock_request):
"""
Test that each session has its own redirect "sandbox".
"""
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {'Location': 'http://www.test.com/redirect_1'}
response1 = Response()
response1.url = 'http://www.test.com/redirect_1'
response1.status_code = 301
response1.headers = {'Location': 'http://www.test.com/redirect_2'}
response2 = Response()
response2.url = 'http://www.test.com/redirect_2'
response2.status_code = 301
response2.headers = {'Location': 'http://www.test.com/redirect_3'}
response3 = Response()
response3.url = 'http://www.test.com/redirect_3'
response3.status_code = 200
response3._content = 'Mocked response content'
response3.history = [response0, response1, response2]
mock_request.return_value = response3
s0 = Session()
s1 = Session()
# s0 make a request
r = s0.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s0 make a request again. Assert we not make request to 301 again.
r = s0.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s1 make a request
r = s1.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s1 make a request again. Assert we not make request to 301 again.
r = s1.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
self.assertEqual(r.status_code, 200)
示例14: get
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def get(self):
r = Response()
try:
r._content = open(self.url).read()
#: Integer Code of responded HTTP Status.
r.status_code = 200
except IOError as e:
r.status_code = 404
raise ConnectionError(e)
r._content_consumed = True
#: Final URL location of Response.
r.url = self.url
#: Resulting :class:`HTTPError` of request, if one occurred.
self.error = None
#: Encoding to decode with when accessing r.content.
self.encoding = None
#: The :class:`Request <Request>` that created the Response.
self.request = self
# Return the response.
return r
示例15: send
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import url [as 别名]
def send(self, request, stream=None, timeout=None, verify=None, cert=None,
proxies=None):
parsed_url = urlparse.urlparse(request.url)
# We only work for requests with a host of localhost
if parsed_url.netloc.lower() != "localhost":
raise InvalidURL("Invalid URL %r: Only localhost is allowed" %
request.url)
real_url = urlparse.urlunparse(parsed_url[:1] + ("",) + parsed_url[2:])
pathname = url_to_path(real_url)
resp = Response()
resp.status_code = 200
resp.url = real_url
stats = os.stat(pathname)
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
resp.headers = CaseInsensitiveDict({
"Content-Type": mimetypes.guess_type(pathname)[0] or "text/plain",
"Content-Length": stats.st_size,
"Last-Modified": modified,
})
resp.raw = LocalFSResponse(open(pathname, "rb"))
resp.close = resp.raw.close
return resp