本文整理匯總了Python中twisted.web.client.PartialDownloadError方法的典型用法代碼示例。如果您正苦於以下問題:Python client.PartialDownloadError方法的具體用法?Python client.PartialDownloadError怎麽用?Python client.PartialDownloadError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.web.client
的用法示例。
在下文中一共展示了client.PartialDownloadError方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_withPotentialDataLoss
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_withPotentialDataLoss(self):
"""
If the full body of the L{IResponse} passed to L{client.readBody} is
not definitely received, the L{Deferred} returned by L{client.readBody}
fires with a L{Failure} wrapping L{client.PartialDownloadError} with
the content that was received.
"""
response = DummyResponse()
d = client.readBody(response)
response.protocol.dataReceived(b"first")
response.protocol.dataReceived(b"second")
response.protocol.connectionLost(Failure(PotentialDataLoss()))
failure = self.failureResultOf(d)
failure.trap(client.PartialDownloadError)
self.assertEqual({
"status": failure.value.status,
"message": failure.value.message,
"body": failure.value.response,
}, {
"status": b"200",
"message": b"OK",
"body": b"firstsecond",
})
示例2: test_downloadPageBrokenDownload
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_downloadPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{downloadPage} fails with L{PartialDownloadError}.
"""
# test what happens when download gets disconnected in the middle
path = FilePath(self.mktemp())
d = client.downloadPage(self.getURL("broken"), path.path)
d = self.assertFailure(d, client.PartialDownloadError)
def checkResponse(response):
"""
The HTTP status code from the server is propagated through the
C{PartialDownloadError}.
"""
self.assertEqual(response.status, b"200")
self.assertEqual(response.message, b"OK")
return response
d.addCallback(checkResponse)
def cbFailed(ignored):
self.assertEqual(path.getContent(), b"abc")
d.addCallback(cbFailed)
return d
示例3: test_downloadPageLogsFileCloseError
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_downloadPageLogsFileCloseError(self):
"""
If there is an exception closing the file being written to after the
connection is prematurely closed, that exception is logged.
"""
class BrokenFile:
def write(self, bytes):
pass
def close(self):
raise IOError(ENOSPC, "No file left on device")
d = client.downloadPage(self.getURL("broken"), BrokenFile())
d = self.assertFailure(d, client.PartialDownloadError)
def cbFailed(ignored):
self.assertEqual(len(self.flushLoggedErrors(IOError)), 1)
d.addCallback(cbFailed)
return d
示例4: test_downloadPageBrokenDownload
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_downloadPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{downloadPage} fails with L{PartialDownloadError}.
"""
# test what happens when download gets disconnected in the middle
path = FilePath(self.mktemp())
d = client.downloadPage(self.getURL("broken"), path.path)
d = self.assertFailure(d, client.PartialDownloadError)
def checkResponse(response):
"""
The HTTP status code from the server is propagated through the
C{PartialDownloadError}.
"""
self.assertEquals(response.status, "200")
self.assertEquals(response.message, "OK")
return response
d.addCallback(checkResponse)
def cbFailed(ignored):
self.assertEquals(path.getContent(), "abc")
d.addCallback(cbFailed)
return d
示例5: test_downloadPageLogsFileCloseError
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_downloadPageLogsFileCloseError(self):
"""
If there is an exception closing the file being written to after the
connection is prematurely closed, that exception is logged.
"""
class BrokenFile:
def write(self, bytes):
pass
def close(self):
raise IOError(ENOSPC, "No file left on device")
d = client.downloadPage(self.getURL("broken"), BrokenFile())
d = self.assertFailure(d, client.PartialDownloadError)
def cbFailed(ignored):
self.assertEquals(len(self.flushLoggedErrors(IOError)), 1)
d.addCallback(cbFailed)
return d
示例6: test_delete_node_continues_on_404_error
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_delete_node_continues_on_404_error(self):
driver = RSDPodDriver()
context = make_context()
url = driver.get_url(context)
node_id = context.get("node_id").encode("utf-8")
endpoint = b"redfish/v1/Nodes/%s" % node_id
headers = driver.make_auth_headers(**context)
mock_redfish_request = self.patch(driver, "redfish_request")
error = PartialDownloadError(
response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
code=HTTPStatus.NOT_FOUND,
)
mock_redfish_request.side_effect = error
yield driver.delete_node(url, node_id, headers)
self.assertThat(
mock_redfish_request,
MockCalledOnceWith(b"DELETE", join(url, endpoint), headers),
)
示例7: test_delete_node_raises_when_not_404_error
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_delete_node_raises_when_not_404_error(self):
driver = RSDPodDriver()
context = make_context()
url = driver.get_url(context)
node_id = context.get("node_id").encode("utf-8")
endpoint = b"redfish/v1/Nodes/%s" % node_id
headers = driver.make_auth_headers(**context)
mock_redfish_request = self.patch(driver, "redfish_request")
error = PartialDownloadError(
response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
code=HTTPStatus.BAD_REQUEST,
)
mock_redfish_request.side_effect = error
with ExpectedException(PartialDownloadError):
yield driver.delete_node(url, node_id, headers)
self.assertThat(
mock_redfish_request,
MockCalledOnceWith(b"DELETE", join(url, endpoint), headers),
)
示例8: test_redfish_request_continues_partial_download_error
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_redfish_request_continues_partial_download_error(self):
driver = RedfishPowerDriver()
context = make_context()
url = driver.get_url(context)
uri = join(url, b"redfish/v1/Systems")
headers = driver.make_auth_headers(**context)
mock_agent = self.patch(redfish_module, "Agent")
mock_agent.return_value.request = Mock()
expected_headers = Mock()
expected_headers.code = HTTPStatus.OK
expected_headers.headers = "Testing Headers"
mock_agent.return_value.request.return_value = succeed(
expected_headers
)
mock_readBody = self.patch(redfish_module, "readBody")
error = PartialDownloadError(
response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
code=HTTPStatus.OK,
)
mock_readBody.return_value = fail(error)
expected_response = SAMPLE_JSON_SYSTEMS
response, headers = yield driver.redfish_request(b"GET", uri, headers)
self.assertEquals(expected_response, response)
self.assertEquals(expected_headers.headers, headers)
示例9: test_redfish_request_raises_failures
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_redfish_request_raises_failures(self):
driver = RedfishPowerDriver()
context = make_context()
url = driver.get_url(context)
uri = join(url, b"redfish/v1/Systems")
headers = driver.make_auth_headers(**context)
mock_agent = self.patch(redfish_module, "Agent")
mock_agent.return_value.request = Mock()
expected_headers = Mock()
expected_headers.code = HTTPStatus.OK
expected_headers.headers = "Testing Headers"
mock_agent.return_value.request.return_value = succeed(
expected_headers
)
mock_readBody = self.patch(redfish_module, "readBody")
error = PartialDownloadError(
response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
code=HTTPStatus.NOT_FOUND,
)
mock_readBody.return_value = fail(error)
with ExpectedException(PartialDownloadError):
yield driver.redfish_request(b"GET", uri, headers)
self.assertThat(mock_readBody, MockCalledOnceWith(expected_headers))
示例10: test_getPageBrokenDownload
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_getPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{getPage} fails with L{PartialDownloadError}.
"""
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEqual(exc.response, b"abc"))
return d
示例11: test_downloadPageLogsFileCloseError
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_downloadPageLogsFileCloseError(self):
"""
If there is an exception closing the file being written to after the
connection is prematurely closed, that exception is logged.
"""
exc = IOError(ENOSPC, "No file left on device")
class BrokenFile:
def write(self, bytes):
pass
def close(self):
raise exc
logObserver = EventLoggingObserver()
filtered = FilteringLogObserver(
logObserver,
[LogLevelFilterPredicate(defaultLogLevel=LogLevel.critical)]
)
globalLogPublisher.addObserver(filtered)
self.addCleanup(lambda: globalLogPublisher.removeObserver(filtered))
d = client.downloadPage(self.getURL("broken"), BrokenFile())
d = self.assertFailure(d, client.PartialDownloadError)
def cbFailed(ignored):
self.assertEquals(1, len(logObserver))
event = logObserver[0]
f = event["log_failure"]
self.assertIsInstance(f.value, IOError)
self.assertEquals(
f.value.args,
exc.args
)
self.assertEqual(len(self.flushLoggedErrors(IOError)), 1)
d.addCallback(cbFailed)
return d
示例12: test_getPageBrokenDownload
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def test_getPageBrokenDownload(self):
"""
If the connection is closed before the number of bytes indicated by
I{Content-Length} have been received, the L{Deferred} returned by
L{getPage} fails with L{PartialDownloadError}.
"""
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEquals(exc.response, "abc"))
return d
示例13: handleResponse
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def handleResponse(self, response):
if self.quietLoss:
return
if self.failed:
self.factory.noPage(
failure.Failure(
error.Error(
self.status, self.message, response)))
elif self.length != None and self.length != 0:
self.factory.noPage(failure.Failure(
client.PartialDownloadError(self.status, self.message, response)))
else:
if self.decode:
s = StringIO()
s.write(response)
s.seek(-1)
g = GzipFile(fileobj=s, mode='rb')
try:
response = g.read()
except IOError:
self.factory.noPage(failure.Failure(
client.PartialDownloadError(self.status, self.message, response)))
self.transport.loseConnection()
return
g.close()
self.factory.page(response)
# server might be stupid and not close connection.
self.transport.loseConnection()
示例14: testBrokenDownload
# 需要導入模塊: from twisted.web import client [as 別名]
# 或者: from twisted.web.client import PartialDownloadError [as 別名]
def testBrokenDownload(self):
# test what happens when download gets disconnected in the middle
d = client.getPage(self.getURL("broken"))
d = self.assertFailure(d, client.PartialDownloadError)
d.addCallback(lambda exc: self.assertEquals(exc.response, "abc"))
return d