本文整理汇总了Python中twisted.web._newclient.ResponseFailed方法的典型用法代码示例。如果您正苦于以下问题:Python _newclient.ResponseFailed方法的具体用法?Python _newclient.ResponseFailed怎么用?Python _newclient.ResponseFailed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web._newclient
的用法示例。
在下文中一共展示了_newclient.ResponseFailed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handleRedirect
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def _handleRedirect(self, response, method, uri, headers, redirectCount):
"""
Handle a redirect response, checking the number of redirects already
followed, and extracting the location header fields.
"""
if redirectCount >= self._redirectLimit:
err = error.InfiniteRedirection(
response.code,
b'Infinite redirection detected',
location=uri)
raise ResponseFailed([Failure(err)], response)
locationHeaders = response.headers.getRawHeaders(b'location', [])
if not locationHeaders:
err = error.RedirectWithNoLocation(
response.code, b'No location header field', uri)
raise ResponseFailed([Failure(err)], response)
location = self._resolveLocation(uri, locationHeaders[0])
deferred = self._agent.request(method, location, headers)
def _chainResponse(newResponse):
newResponse.setPreviousResponse(response)
return newResponse
deferred.addCallback(_chainResponse)
return deferred.addCallback(
self._handleResponse, method, uri, headers, redirectCount + 1)
示例2: test_onlyRetryIfNoResponseReceived
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_onlyRetryIfNoResponseReceived(self):
"""
Only L{RequestNotSent}, L{RequestTransmissionFailed} and
L{ResponseNeverReceived} exceptions cause a retry.
"""
pool = client.HTTPConnectionPool(None)
connection = client._RetryingHTTP11ClientProtocol(None, pool)
self.assertTrue(connection._shouldRetry(
b"GET", RequestNotSent(), None))
self.assertTrue(connection._shouldRetry(
b"GET", RequestTransmissionFailed([]), None))
self.assertTrue(connection._shouldRetry(
b"GET", ResponseNeverReceived([]),None))
self.assertFalse(connection._shouldRetry(
b"GET", ResponseFailed([]), None))
self.assertFalse(connection._shouldRetry(
b"GET", ConnectionRefusedError(), None))
示例3: test_noLocationField
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_noLocationField(self):
"""
If no L{Location} header field is found when getting a redirect,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping a
L{error.RedirectWithNoLocation} exception.
"""
deferred = self.agent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers()
response = Response((b'HTTP', 1, 1), 301, b'OK', headers, None)
res.callback(response)
fail = self.failureResultOf(deferred, client.ResponseFailed)
fail.value.reasons[0].trap(error.RedirectWithNoLocation)
self.assertEqual(b'http://example.com/foo',
fail.value.reasons[0].value.uri)
self.assertEqual(301, fail.value.response.code)
示例4: _testPageRedirectFailure
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def _testPageRedirectFailure(self, code, method):
"""
When getting a redirect on an unsupported request method,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping
a L{error.PageRedirect} exception.
@param code: HTTP status code.
@param method: HTTP request method.
"""
deferred = self.agent.request(method, b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers()
response = Response((b'HTTP', 1, 1), code, b'OK', headers, None)
res.callback(response)
fail = self.failureResultOf(deferred, client.ResponseFailed)
fail.value.reasons[0].trap(error.PageRedirect)
self.assertEqual(b'http://example.com/foo',
fail.value.reasons[0].value.location)
self.assertEqual(code, fail.value.response.code)
示例5: test_someResponseButNotAll
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_someResponseButNotAll(self):
"""
If a partial response was received and the connection is lost, the
resulting error is L{ResponseFailed}, but not
L{ResponseNeverReceived}.
"""
protocol = HTTPClientParser(
Request(b'HEAD', b'/', _boringHeaders, None),
lambda ign: None)
d = protocol._responseDeferred
protocol.makeConnection(StringTransport())
protocol.dataReceived(b'2')
protocol.connectionLost(ConnectionLost())
return self.assertFailure(d, ResponseFailed).addCallback(
self.assertIsInstance, ResponseFailed)
示例6: dataReceived
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def dataReceived(self, data):
"""
Decompress C{data} with the zlib decompressor, forwarding the raw data
to the original protocol.
"""
try:
rawData = self._zlibDecompress.decompress(data)
except zlib.error:
raise ResponseFailed([Failure()], self._response)
if rawData:
self.original.dataReceived(rawData)
示例7: connectionLost
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def connectionLost(self, reason):
"""
Forward the connection lost event, flushing remaining data from the
decompressor if any.
"""
try:
rawData = self._zlibDecompress.flush()
except zlib.error:
raise ResponseFailed([reason, Failure()], self._response)
if rawData:
self.original.dataReceived(rawData)
self.original.connectionLost(reason)
示例8: _handleResponse
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def _handleResponse(self, response, method, uri, headers, redirectCount):
"""
Handle the response, making another request if it indicates a redirect.
"""
if response.code in self._redirectResponses:
if method not in (b'GET', b'HEAD'):
err = error.PageRedirect(response.code, location=uri)
raise ResponseFailed([Failure(err)], response)
return self._handleRedirect(response, method, uri, headers,
redirectCount)
elif response.code in self._seeOtherResponses:
return self._handleRedirect(response, b'GET', uri, headers,
redirectCount)
return response
示例9: test_307OnPost
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_307OnPost(self):
"""
When getting a 307 redirect on a I{POST} request,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping
a L{error.PageRedirect} exception.
"""
self._testPageRedirectFailure(307, b'POST')
示例10: test_redirectLimit
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_redirectLimit(self):
"""
If the limit of redirects specified to L{client.RedirectAgent} is
reached, the deferred fires with L{ResponseFailed} error wrapping
a L{InfiniteRedirection} exception.
"""
agent = self.buildAgentForWrapperTest(self.reactor)
redirectAgent = client.RedirectAgent(agent, 1)
deferred = redirectAgent.request(b'GET', b'http://example.com/foo')
req, res = self.protocol.requests.pop()
headers = http_headers.Headers(
{b'location': [b'http://example.com/bar']})
response = Response((b'HTTP', 1, 1), 302, b'OK', headers, None)
res.callback(response)
req2, res2 = self.protocol.requests.pop()
response2 = Response((b'HTTP', 1, 1), 302, b'OK', headers, None)
res2.callback(response2)
fail = self.failureResultOf(deferred, client.ResponseFailed)
fail.value.reasons[0].trap(error.InfiniteRedirection)
self.assertEqual(b'http://example.com/foo',
fail.value.reasons[0].value.location)
self.assertEqual(302, fail.value.response.code)
示例11: test_302OnPost
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_302OnPost(self):
"""
When getting a 302 redirect on a I{POST} request,
L{client.RedirectAgent} fails with a L{ResponseFailed} error wrapping
a L{error.PageRedirect} exception.
"""
self._testPageRedirectFailure(302, b'POST')
示例12: assertResponseFailed
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def assertResponseFailed(self, deferred, reasonTypes):
"""
A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
of L{ResponseFailed}.
"""
return assertWrapperExceptionTypes(self, deferred, ResponseFailed, reasonTypes)
示例13: test_abortBeforeResponseBody
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_abortBeforeResponseBody(self):
"""
The Deferred returned by L{HTTP11ClientProtocol.request} will fire
with a L{ResponseFailed} failure containing a L{ConnectionAborted}
exception, if the connection was aborted before all response headers
have been received.
"""
transport = StringTransport()
protocol = HTTP11ClientProtocol()
protocol.makeConnection(transport)
result = protocol.request(Request(b'GET', b'/', _boringHeaders, None))
protocol.abort()
self.assertTrue(transport.disconnecting)
protocol.connectionLost(Failure(ConnectionDone()))
return assertResponseFailed(self, result, [ConnectionAborted])
示例14: test_cancelDuringResponse
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def test_cancelDuringResponse(self):
"""
The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
with a L{ResponseFailed} failure containing a L{CancelledError}
exception if the request was cancelled before all response headers were
received.
"""
transport = StringTransport()
protocol = HTTP11ClientProtocol()
protocol.makeConnection(transport)
result = protocol.request(Request(b'GET', b'/', _boringHeaders, None))
protocol.dataReceived(b"HTTP/1.1 200 OK\r\n")
result.cancel()
self.assertTrue(transport.aborting)
return assertResponseFailed(self, result, [CancelledError])
示例15: trap_connection_err
# 需要导入模块: from twisted.web import _newclient [as 别名]
# 或者: from twisted.web._newclient import ResponseFailed [as 别名]
def trap_connection_err(self, fail):
fail.trap(ConnectError, ConnectionClosed, ResponseFailed,
DNSLookupError)