本文整理匯總了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)